I am trying to do the following exercice:
Given a DNA string, compute how many times each nucleotide occurs in the string.
DNA is represented by an alphabet of the following symbols: 'A', 'C', 'G', and 'T'.
Here is a simple unit test to better understand what's expected:
(ert-deftest count-all-nucleotides-test ()
(should (equal (sort-pairs (nucleotide-count "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC") #'<)
'((?A . 20) (?C . 12) (?G . 17) (?T . 21)))))
Here is the function I've written:
(defun nucleotide-count (s)
"Return the count of nucleotides in S."
(let ((result '((?A . 0) (?C . 0) (?G . 0) (?T . 0))))
(seq-reduce
(lambda (result c)
(let ((count (cdr (assoc c result))))
(if (equal c nil)
(error (format "Invalid nucleotide %c" c))
(setf (cdr (assoc c result)) (1+ count)))
result))
s
result)))
This seems to work... but only once. It seems the function re-uses the previous alist and updates it but I don't understand why:
ELISP> (nucleotide-count "ACGT")
((65 . 1)
(67 . 1)
(71 . 1)
(84 . 1))
ELISP> (nucleotide-count "ACGT")
((65 . 2)
(67 . 2)
(71 . 2)
(84 . 2))
ELISP> (nucleotide-count "ACGT")
((65 . 3)
(67 . 3)
(71 . 3)
(84 . 3))
Why is that?