scheme - Is ((1 2) 3) the same as ((1 2) . 3)? -


i've been given assignment in scheme (using drracket) asks me create list ((1 2) 3) using cons, components 1, 2, 3 , ()

i've managed far ((1 2) . 3) using:

(cons (cons '1 (cons '2 '())) '3) 

however, same thing?

note: cannibalized my answer recursive range in lisp adds period? question different (so not duplicate), background explanation of how lists constructed cons cells, , how they're printed same. end bit different.

a list in scheme either empty list () (also known nil in lisps), or cons cell car (also known first) element of list , cdr (also known rest) either rest of list (i.e., list), or atom terminates list. conventional terminator empty list (); lists terminated () said "proper lists". lists terminated other atom called "improper lists". list (1 2 3 4 5) contains elements 1, 2, 3, 4, , 5, , terminated (). construct

(cons 1 (cons 2 (cons 3 (cons 4 (cons 5 ()))))) 

now, when system prints cons cell, general case print

(car . cdr) 

for instance, result of (cons 1 2) printed

(1 . 2) 

since lists built of cons cells, can use notation lists too:

'(1 2 3 4 5)                      ; == '(1 . (2 . (3 . (4 . (5 . ()))))) 

that's rather clunky, though, lisps (all know of) have special case printing cons cells: if cdr list (either cons cell, or ()), don't print ., , don't print surrounding parenthesis of cdr (which otherwise have, since it's list).

with understanding of lists , cons cells, can consider specific cases ((1 2) 3) , ((1 2) . 3). let's break each of these down cons cells.

((1 2) 3)                              ; == ((1 . (2 . ()) . (3 . ()))             ; == (cons (cons 1 (cons 2 ()) (cons 3 ()))   
((1 2) . 3)                   ; ==            ((1 . (2 . ())) . 3)          ; == (cons (cons 1 (cons 2 ())) 3) 

the last lines in each case different, ((1 2) 3) , ((1 2) . 3) not same.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -