hitish
hitish

Reputation: 355

Whats the difference between '((1 2)(3 4)) and '('(1 2 )'(3 4)) in lisp?

The length of

(length (car '('(0)'(1 2 3)'(6 7))))

is being show 2 while the same

(length (car '((0)(1 2 3)(6 7))))

is correctly shown 1. so what does the the first expression actually represent?

Upvotes: 2

Views: 567

Answers (2)

Rainer Joswig
Rainer Joswig

Reputation: 139241

CL-USER 8 > '((0)(1 2 3)(6 7))
((0)
 (1 2 3)
 (6 7))

CL-USER 9 > '('(0)'(1 2 3)'(6 7))
((QUOTE (0))
 (QUOTE (1 2 3))
 (QUOTE (6 7)))

Upvotes: 1

Barmar
Barmar

Reputation: 780673

The quote character ' is a reader macro. 'anything expands into (quote anything). So the first expression is shorthand for:

(length (car (quote ((quote (0))
                     (quote (1 2 3))
                     (quote (6 7)))

The first quote causes the parameter to be treated literally. That means that the quote expressions inside it are just lists that happen to begin with the symbol quote.

So the CAR of that list is the sub-list (quote (0)). It contains 2 elements: the symbol quote and the list (0).

Upvotes: 7

Related Questions