Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

symbols - about the dot "." in scheme

I saw there are other questions about the dot "." I followed but it didn't work for my code.... it's a part of code, the implementation is not focused to this symbol. but output should be included this dot. when I give input of two lists '(1 2 3) '(4 5) my expected output => (1 . 4) (2 . 5)

I managed to get (1 4) (2 5) just need to add "." in the middle.

Part of mycode 
(cons (list (car lst1) (car lst2))
....

for the "." symbol , if I try

**trial-1**
 (cons '(list (car lst1) (car lst2)) ...)

then the output : ((list (car lst1) (car lst2))

**trail-2**
(cons (list (car lst1) '. (car lst2)) ...)

then.. it says : illegal use of `.'

what are the rules to use the dot? any documents I can look at? btw, I am using Racket(R5RS).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The dot symbol is displayed when you build a cons-pair or a list which is not proper (meaning: it doesn't end with the empty list). For example:

(cons 1 2) 
=> (1 . 2) ; a cons-pair

(cons 1 (cons 2 (cons 3 4)))
=> '(1 2 3 . 4) ; an improper list

For instance, to display an output such as the one shown in the question try this:

(define lst1 '(1 2 3))
(define lst2 '(4 5))

(list (cons (car lst1) (car lst2))
      (cons (cadr lst1) (cadr lst2)))

=> '((1 . 4) (2 . 5))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...