Drew
Drew

Reputation: 2663

Basic LISP Issue

So (car '(2 3)) -> 2

(cdr '(2 3)) -> (3)

Which function should I use to be able to get something to yield 3?

(function-name '(2 3)) -> 3

Upvotes: 1

Views: 90

Answers (3)

Mars
Mars

Reputation: 8854

You could also use (second '(2 3)). second is another name for cadr.

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180918

Hints:

car refers to the first element in the list.

cdr refers to the remainder of the list, and is itself a list.

So what you need is a function that returns the first element from a list containing the last element.

Upvotes: 2

Kirby
Kirby

Reputation: 3719

It should be fine to simply do:

(car (cdr '(2 3)))

Which is the same as:

(cadr '(2 3))

This works because "car" gets the first element in the expression, whereas cdr returns the remainder of the list, without the first element. You've already shown that "(cdr '(2 3))" returns a list of "(3)". Therefore, the "car" of this is the element (not the list), "3". By the way, the "(cdr (cdr ('2 3)))" is the "(cdr (3))", which is "()".

Isn't LISP fun?

Upvotes: 7

Related Questions