1
votes

I am starting with Racket and I want to display the value of this function that adds the cdrs of a list of cons, in Racket:

(define (add-cdrs '((a . 1)(a . 2)(a . 3)(a . 4)))
(if (null? l)
    0
    (+ (cdr(car l))(add-cdrs(cdr l)))))

The output should be: 10

But, I don't know how to do it or where to put the display function.

Thank you

1

1 Answers

3
votes

You're confusing procedure definition (which in this case should declare a parameter that will be used to hold a list) with procedure invocation (which binds an actual list to the parameter). Other than that, your logic is correct. Try this:

(define (add-cdrs lst)
  (if (null? lst)
      0
      (+ (cdr (car lst)) (add-cdrs (cdr lst)))))

(add-cdrs '((a . 1) (a . 2) (a . 3) (a . 4)))
=> 10