;; An association list (al) is either
;; empty or
;; (cons (list k v) alst), where
;; k is a nat (the key),
;; v is a string (the value), and
;; alst is an association list (al)
updatestring takes an association list, a number (findnum) and a string (newstring) and if there is a number the same as findnum in the association list, then it replaces the string in the list with newstring.
(check-expect(updatestring empty 3 "hi") (list (list 3 "hi")))
(check-expect(updatestring (list (list 1 "hi")(list 5 "wow")) 5 "new")(list (list 1 "hi")(list 5 "new")))
(check-expect(updatestring (list (list 1 "hi")(list 5 "wow")) 2 "nice")(list (list 2 "nice") (list 1 "hi")(list 5 "wow")))
I'm having trouble with the code as this is what I have.
(define (al-update alst akey avalue)
(cond
[(empty? alst) (list (list akey avalue))]
[(= (first(first alst)) akey) (al-update (rest alst) akey avalue)]
[else (list(list akey avalue alst))]))
The problem is that my code returns
(list (list 5 "new" (list (list 1 "hi") (list 5 "wow"))) instead of (list (list 1 "hi") (list 5 "new"))
and
(list(list 2 "nice" (list (list 1 "hi") (list 5 "wow")))) instead of (list (list 2 "nice") (list 1 "hi")(list 5 "wow")))
Any tips and answers would be very much appreciated thanks!