I’m doing the Allergies and have this code:
(setq allergens '(("eggs" . 1)
("peanuts" . 2)
("shellfish" . 4)
("strawberries" . 8)
("tomatoes" . 16)
("chocolate" . 32)
("pollen" . 64)
("cats" . 128)))
(defun allergic-to-p (score allergen)
"Returns true if given allergy score includes given allergen."
(when (>
(logand
score
(or (cdr (assoc allergen allergens :test #'equal)) 0))
0)
t))
(defun list (score)
"Returns a list of allergens for a given allergy score."
(reduce #'(lambda (acc allergen)
(if (allergic-to-p score (car allergen))
(cons (car allergen) acc)
acc))
allergens
:initial-value ()))
This produces the correct result but in reverse. So I want to change list
to append to the list instead of prepending. To do this I tried to change (cons (car allergen) acc)
to (append acc (list (car allergen)))
. But this gives me an error:
The value
"eggs"
is not of type
INTEGER
[Condition of type TYPE-ERROR]
What am I missing?