0

;; 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!

2 Answers 2

1

Your recursive structure needs to be something like this:

(define (al-update alist akey avalue)
  (if (empty? alist)                                 ; base, end recusion
      '()
       (cons (let ((key+value (first alist)))        ; element, one-by-one
               (if (= ...)
                    ...
                    ...))
             (al-update (rest alist) akey avalue)))) ; recursion

In your code there are a number of problems. The else clause needs to recurse over the rest of the list. In the = clause you need to do the substitution and recurse. In my code above, I combined the two clauses that recurse.

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately I haven't learned cdr or car or null? so I can't use them in my code. Is there another way to do it? Thanks!
i can use cons but not push
I don't quite understand this line (cons (let ((key+value (first alist)))
Cons takes two arguments; the second is a list, the first is something to add to the list. In this case, the result of the let is added to the list. The result of the let is the result of the if which is one of two things. If (= ...) [you provide ...] is true then you want to substitute avalue, if false, then you want to use the old value. The identifier key+value is just a fancy Scheme symbol; it represents the first element in alist.
0

It says "if there is a number the same as findnum in the association list, then it replaces the string in the list with newstring", but in your example if the given number does not exist you add that into the list.

(check-expect (updatestring (list (list 1 "hi") (list 5 "wow")) 2 "nice")
              (list (list 2 "nice") (list 1 "hi") (list 5 "wow")))
(check-expect(updatestring empty 3 "hi") (list (list 3 "hi")))

Code and the examples (tests) should be these:

(define (al-update alst akey avalue)
 (cond
  [(empty? alst) empty]
  [(= (first (first alst)) akey) (cons (list (first (first alst)) avalue)
                                       (al-update (rest alst) akey avalue))]
  [else (cons (first alst) (al-update (rest alst) akey avalue))]))


(check-expect (updatestring empty 3 "hi") empty)
(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 1 "hi") (list 5 "wow")))
(check-expect (updatestring (list (list 1 "hi") (list 5 "wow") (list 2 "bad")) 2 "nice")
              (list (list 1 "hi") (list 5 "wow") (list 2 "nice")))

By the way "car" is equivalent to "first" and "cdr" is "rest".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.