How to create (d . nil) in lisp - lisp

I am beginner in Lisp. There is a question that I cannot solve.
Show the simplest expression that Lisp will print out when you type the following
expression:
’(a b (c . e) (d . nil))
I tried (cons 'a (cons 'b (cons (cons 'c 'e) (cons (cons 'd nil)))))
However, it wouldn't create (d . nil).
So, is there any way to create such a dot pair?

Show the simplest expression that Lisp will print out when you type the following expression
The question does not ask you to create a dotted pair, but how the dotted pair is printed. I suppose this question aims at showing you that lists are just dotted pairs printed in a special way.
If you write this in a REPL:
'(a b (c . e) (d . nil))
The REPL replies:
(A B (C . E) (D))
You have the exact same output if you write:
'(a . (b . ((c . e) . ((d . nil) . nil))))
Basically, (a b) is the list made of a symbol a in front of a sublist (b), written (a . (b)). Here I am only talking about how forms are read. If you write them directly in the REPL, they will first be read, then evaluated, and in that case you will get an error. That's why in the examples are quoted: an expression being quoted is not evaluated, but returned as-is.
If you want to write code that, when evaluated, produces the cons-cell (0 . 1), you have to write this list:
(cons 0 1)
The above is a list of three elements, which could be written equally:
(cons . (0 . (1 . nil)))
Your interpreter will read this list (a symbol, two numbers) and evaluate the resulting form. That forms produces, at runtime, the following cons cell:
(0 . 1)

Related

How to form a list with the elements given by Racklog recursively?

I am starting to read about Racklog, which is somewhat the logic programming extension to Racket, and so far I know that I can great predicates and goals in the following way:
(define %coffee
(%rel ()
[('moka)]
[('turkish)]
[('expresso)]
[('cappuccino)]
[('latte)]))
and when I use the instruction %which and %more starts to show if the goals are fulfilled or not, like the following:
(%which (c) (%coffee c))
((c . moka))
(%more)
((c . turkish))
Until it reaches #f when there are no more predicates to evaluate. So I was wondering if I could extract the results from these predicates evaluation and to form a list recursively. I have programmed the following:
(define (test data)
(if (eq? data #f)
'()
(cons (%which (c) (%coffee c))(test (%more)))))
(test %coffee)
However, it enters an infinite loop: how I can form a list with the results of %more?
%more retries the goal in the last %which-query for a different solution, so you have to call (%which (c) (%coffee c)) only once and then call repeatedly (%more) to get other results:
#lang racket
(require racklog)
(define %coffee
(%rel ()
[('moka)]
[('turkish)]
[('expresso)]
[('cappuccino)]
[('latte)]))
(define (test data)
(if data
(cons data (test (%more)))
'()))
(test (%which (c) (%coffee c)))
Result:
'(((c . moka)) ((c . turkish)) ((c . expresso)) ((c . cappuccino)) ((c . latte)))

Order Lists by CAR

I need to be able to compare two cars of a list to sort them im LISP.
Lists '(e d) (a b)
I want to compare the cars (e and a). This works using eql. If they don't match, I want to order the lists alphabetically, so (a b) (e d).
I'm missing the part where I can see which character is 'bigger', so the check if e or a should come first. I've tried converting them to ascii codes, but that doesn't work for (car a). Using arithmetic operators such as '<' and '>' also doesn't work. Does anyone have an idea on how to do this?
Use string> without symbol-name:
CL-USER 6 > (string> 'a 'b)
NIL
CL-USER 7 > (string< 'a 'b)
0
For the sake of completeness, here is how you should use it inside sort to achieve desired result (sort is destructive- modifies used sequence, so I also used copy-tree to avoid that effect):
(let ((data '((e d) (a b))))
(sort (copy-tree data)
(lambda (x y) (string< (car x) (car y)))))
((A B) (E D))
A symbol is distinct from a string.
CL-USER> (symbol-name 'foo)
"FOO"
A string (a sequence of characters) can be compared in the manner you seem to be interested in.
CL-USER> (string> "FOO" "BOO")
0
CL-USER> (string< "FOO" "BOO")
NIL

How is each of the things different in cons of LISP?

These are the outputs of different combinations of arguments to cons. I just started learning lisp. Can someone help me understand these ?
Break 80 [81]> (CONS '(A) 'B)
((A) . B)
Break 80 [81]> (CONS '(A) '(B))
((A) B)
Break 80 [81]> (CONS 'A 'B)
(A . B)
Break 80 [81]> (CONS 'A '(B))
(A B)
Break 80 [81]>
The cons function always does the same thing: it produces a cons cell of its arguments. A cons cell is simply a pair. You can get the first element of the pair with car and the second element with cdr.
For writing a literal cell, you can quote and use the notation (x . y) where x is the car and y is the cdr. Using this notation, you can write your examples as follows (but don't just take my word for it, try it in the REPL):
> '((A) . B ) ;=> ((A) . B)
> '((A) . (B)) ;=> ((A) B)
> '( A . B ) ;=> (A . B)
> '( A . (B)) ;=> (A B)
But why don't the results of the second and fourth cases print the same way that we wrote them? There are special printing conventions for cons cells, because we use cons cells to implement linked lists.
Read 14.1.2 Conses as Lists for specifics, but a list is a cons cell whose car is the first element of the list, and whose cdr is the rest of the list (i.e., another list). That means that the list (1 2 3) is the same as (using the literal dotted pair notation) (1 . (2 . (3 . nil))). You can try that in the REPL:
> '(1 . (2 . (3 . nil))) ;=> (1 2 3)
That's a very useful printing (and input) convention for a language where lists are so fundamental. I've written more about this in an answer to other questions, too:
Dot notation in scheme
Recursive range in Lisp adds a period?

LISP: Help reading input stream (with spaces) into a list of characters

I am attempting to write a program whereby I need to process a string character-by-character, including whitespace. This string would be given by the user running the program, from the standard input stream.
I have noticed that the (read) function stops only after some non-whitespace characters have been captured, and cuts off everything starting with the first whitespace character.
In other words if I do a (read) function and enter hi to you, it will return HI. What I would like my function to return is (#\h #\i #\Space #\t #\o #\Space #\y #\o #\u). Can anyone give me some pointers as to how to accomplish this? Sorry guys, I am VERY new to LISP but I like it a lot.
EDIT: One thing I've tried is using (cons) with (read-char). For example: (cons (read-char) (cons (read-char) (cons (read-char) (cons (read-char) (read-char))))). When the user enters "hello", this outputs (#\h #\e #\l #\l . #\o), which is close, but what is that extra period doing in there?
Xach basically answered your main question: you'd probably want to read a line with read-line and coerce it to a list: (coerce (read-line) 'list) and then do something with this list.
The reason that read doesn't work is that it reads and returns s-expressions:
> (car (read))
(foo bar baz) ; this is me typing
FOO
So it just reads the first symbol hi in the input "hi to you" and stops. Successive calls to read will return the rest:
> (list (read) (read) (read))
hi to you ; me typing again
(HI TO YOU)
You probably still want to use Xach's suggestion to get your input though.
To address your other question, the reason there's a dot in that list of characters is that the expression (cons (read-char) (cons (read-char) (cons (read-char) (cons (read-char) (read-char))))) ends with (cons (read-char) (read-char)) which conses two characters together.
Cons creates "cons cells" or Lisp pairs, which are represented as a dotted pair:
> (cons 'a 'b)
(A . B)
Lisp lists are usually formed with nil or the empty list as the last element. But they are actually represented as linked lists with these cons cells, these are the same:
(a . (b . (c . ())))
(a b c)
They are just usually printed the second way. In your case, the last element is a character, so it's printed as a dotted tail list. Experiment with it at the REPL and it will make sense:
> '(a . (b . c))
(A B . C)
> '(a b . c)
(A B . C)
> '(a b c)
(A B C)
> '(a . (b . (c . nil)))
(A B C)
So what you would want to do with that original expression is this:
> (cons (read-char) (cons (read-char) (cons (read-char) (cons (read-char) (cons (read-char) nil)))))
(#\h #\e #\l #\l #\o)
read-char will return the next available character, and read-line will return an entire line of input as a string. You can use coerce to change a string to a list of characters, e.g.
(coerce "foo" 'list) => (#\f #\o #\o)

How to do ((A.B).(C.D)) in lisp

I'm trying to figure out how to do this using cons:
((A . B) . (C . D))
where (A . B) and (C . D) are in each cons cell
I've tried doing this (cons (cons 'a 'b) (cons 'c 'd)) but it gives me this:
((A.B) C . D)
I also tried this: (cons (cons 'a 'b) (cons (cons 'c 'd) ())) but it gives me this:
((A . B) (C . D))
Any idea how to achieve this?
The first one is what you want. They're equivalent. You can verify like this:
1 ]=> (cons (cons 'a 'b) (cons 'c 'd))
;Value 11: ((a . b) c . d)
1 ]=> (car (cons (cons 'a 'b) (cons 'c 'd)))
;Value 12: (a . b)
1 ]=> (cdr (cons (cons 'a 'b) (cons 'c 'd)))
;Value 13: (c . d)
Remember a list is a cons cell. The "car" is the head element of the list or the first half of the cons cell, and the cdr is the rest of the list, or the second element of the cons cell.
Another way to verify that they're equivalent:
1 ]=> '((a . b) . (c . d))
;Value 14: ((a . b) c . d)
Just look at what you get back when you enter in a literal ((A . B) . (C . D)):
* '((a . b) . (c . d))
((A . B) C . D)
There is a defined algorithm the Lisp printer uses to print out data structures built from pairs. Basically, you can't ever get a cons to be printed as a dotted pair inside parentheses when it is the CDR of another cons.
However, it is possible to re-configure the printer so that you get the behavior you are seeking, via SET-PPRINT-DISPATCH:
(set-pprint-dispatch 'cons
(lambda (stream object)
(format stream "(~W . ~W)" (car object) (cdr object))))
* '((a . b) . (c . d))
((A . B) . (C . D))
* (cons (cons 'a 'b) (cons 'c 'd)) ;The same object
((A . B) . (C . D))
Although in spite of that it would frankly be better in the long run if you got comfortable with reading the default behavior.
I'm not quite sure what you mean... I agree with the above comment that the last line of your code resembles the first, which you are matching against.
Here's a decent general resource for you anyhow: http://www-2.cs.cmu.edu/~dst/LispBook/
What you're looking for isn't possible because of how lists are represented in Lisp. When you create a list, you are creating a series of cons cells, where the car of the cell is the value of that element in the list, and the cdr is a reference to the next cons cell. Your desired cell, ((A . B) . (C . D)) means "create a cons cell where the car is (A . B) and the cdr is (C . D)". That is equivalent to a list where the first element is (A . B), second element is C and the tail of the list is D, or ((A . B) C . D).