Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about the dot "." in scheme

Tags:

symbols

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

like image 525
user1915570 Avatar asked Oct 24 '25 19:10

user1915570


1 Answers

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))
like image 177
Óscar López Avatar answered Oct 27 '25 00:10

Óscar López



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!