Chris R
Chris R

Reputation: 735

Definition of a list in Scheme

Finally taking the plunge to learn a Lisp dialect (Scheme), I have encountered two definitions of a list -

"Either the empty list or a pair whose cdr is a list".

"A collection of S-Expressions enclosed by parentheses".

Are these definitions equivalent?

Upvotes: 1

Views: 568

Answers (2)

C. K. Young
C. K. Young

Reputation: 223183

I'm going to bring out my favourite dog-and-pony show!

Tree of cons cells
(source: hedgee.com)

This corresponds to the following:

(let ((s5 (sqrt 5)))
  (/ (- (expt (/ (1+ s5) 2) n)
        (expt (/ (- 1 s5) 2) n)) s5))

The diagram illustrates your first statement (the empty-list object is denoted as a black box). The code snippet illustrates your second. :-)

Upvotes: 4

corsiKa
corsiKa

Reputation: 82589

They're as equivalent as {'a','b','c'} and "abc"

The former is the machine's logical representation of a list, the latter is how you represent it in your code.

And in scheme, you can pretty much treat everything as a list :) (Someone's going to downvote me for that, but I found it to be true when trying to think scheme-esque.)

Upvotes: 4

Related Questions