Aspen van Wood
Aspen van Wood

Reputation: 33

How do you perform a recursion on the rest of the first of a list? (Racket)

If you have a list of list of lists, how do you access the rest of the first of the list?

e.g. If you have

(define l1 (list (list (list 1 1) (list 2 3) (list 7 8))
            (list (list 2) (list 3 4 5))))

How would you perform a recursion on this part

(list (list 1 1) (list 2 3) (list 7 8))

When I try (rest (first l1))

e.g. (map add1 (rest (first l1))

I get an error add1: expects a number; given (list 2 3)

Upvotes: 1

Views: 228

Answers (1)

John Clements
John Clements

Reputation: 17203

You're exactly right that the code

(rest (first l1))

produces the rest of the first of the list. Specifically:

(define l1 (list (list (list 1 1) (list 2 3) (list 7 8))
            (list (list 2) (list 3 4 5))))

(rest (first l1))

produces

(list (list 2 3) (list 7 8))

It's true that you can't add the elements of this list together, because they're not numbers.

Does this answer your question?

Upvotes: 2

Related Questions