I have a project about huffman coding, and I am stuck, I don't understand why my code is not working.
This is the exercise:
Write a function
add1
which, given a character, adds 1 to its frequency in a frequency list. If the character is not yet in the list of frequencies, it is added.
(add1 "e" '(("l" 1) ("e" 2) ("x" 1))) → (("l" 1) ("e" 3) ("x" 1))
(add1 "e" '(("a" 4) ("b" 3))) → (("a" 4) ("b" 3) ("e" 1))
What I wrote:
(define add1
(lambda (c l)
(if (null? l)
'()
(if (member? c l)
(if (equal? c (caar l))
(+ 1 (cadar l))
(add1 c (cdr l)))
(append l '((c 1)))))
))
The result:
(list (list "l" 1) (list "e" 2) (list "x" 1) (list 'c 1))