Skip to content
This repository was archived by the owner on Feb 11, 2018. It is now read-only.

Add hamming exercise #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add hamming exercise #4

wants to merge 1 commit into from

Conversation

mbertheau
Copy link
Owner

No description provided.

@lexi-lambda
Copy link

Instead of returning #f on invalid input, this should throw an exception. Also, the loop could be built more elegantly using for/sum. Finally, you should use eqv? with characters, not eq?.

(define (hamming-distance dna1 dna2)
  (unless (= (string-length dna1) (string-length dna2))
    (raise-argument-error 'hamming-distance "strings of equal length" 1 dna1 dna2))
  (for/sum ([c1 (in-string dna1)]
            [c2 (in-string dna2)])
    (if (eqv? c1 c2) 0 1)))

You can add tests for the bad cases using check-exn.

(check-exn #rx"strings of equal length"
           (thunk (hamming-distance "AATG" "AAA"))
           "disallow first strand longer")

@mbertheau mbertheau force-pushed the hamming branch 3 times, most recently from d6f9d6e to 2755324 Compare May 14, 2015 23:31
@mbertheau
Copy link
Owner Author

Hmm, @lexi-lambda what do you think about this suggestion by zeodary? I think it expresses the idea of counting differences more directly.

@mbertheau mbertheau force-pushed the hamming branch 2 times, most recently from 951e9c0 to 5346664 Compare May 22, 2015 21:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants