开发者

Definition of a list in Scheme

开发者 https://www.devze.com 2023-02-10 13:22 出处:网络
Finally taking the plunge to learn a Lisp dialect (Scheme), I have encountered two definitions of a list -

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

&qu开发者_高级运维ot;Either the empty list or a pair whose cdr is a list".

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

Are these definitions equivalent?


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.)


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

Definition of a list in Scheme


(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. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消