Take the 2-minute tour ×

So, I'm trying to add up the sublists of a list. Like, if I have something like this:

(add-pair '((1 4) (2 1)))

I want it to return this:

(5 3)

This is what I have so far:

(define pair-additions
  (lambda (ls)
      (lambda (n)
        (+ (car n) (cdr n)))ls)))

Right now, it's giving me an error saying that the input isn't a number. Can anybody help me out?

share|improve this question
Note you could also use apply here: (map (lambda (x) (apply + x)) '((1 4) (2 1))) ;=> (5 3) –  A. Webb Mar 5 '13 at 4:00
add comment

1 Answer

up vote 0 down vote accepted

You almost got it! here is the problem:

(+ (car n) (cdr n))

For retrieving the second element of a list, you have to take the car of the cdr, not just the cdr. Change the above line for this:

(+ (car n) (car (cdr n)))

Or for this, which is a short form of the previous line:

(+ (car n) (cadr n))

Or for this, which is easier to read (if supported by your Scheme interpreter):

(+ (first n) (second n))

All the above are equivalent.

share|improve this answer
Cool. I was close. Thanks for your help! –  taylor18 Mar 5 '13 at 3:45
add comment

Your Answer


