Can you get dot representation using 'list' command in LISP? - lisp

I understand that by using cons we could have something like :
> (cons 'b 'c)
(B . C)
And that happens because the cell is split into two and the values are B and C.
My question is, can you get the same result using list?

You can't get list to return a dotted list, because "the last argument to list becomes the car of the last cons constructed" in the returned list.
But, you can get list* to return a dotted list. With the list* function, the final argument becomes the cdr of the last cons, so:
CL-USER> (list* 'a '(b))
(A B)
CL-USER> (list* 'a 'b '())
(A B)
CL-USER> (list* 'a 'b)
(A . B)
CL-USER> (list* 'a '(b c))
(A B C)
CL-USER> (list* 'a 'b '(c))
(A B C)
CL-USER> (list* 'a 'b 'c '())
(A B C)
CL-USER> (list* 'a 'b 'c)
(A B . C)
For example, (list* 'a 'b 'c '()) and (list 'a 'b 'c) are both equivalent to:
CL-USER> (cons 'a (cons 'b (cons 'c '())))
(A B C)
But (list* 'a 'b 'c) is equivalent to:
CL-USER> (cons 'a (cons 'b 'c))
(A B . C)
And, (list* 'a 'b) is equivalent to:
CL-USER> (cons 'a 'b)
(A . B)

No you can't, because a list is a list of cons cells whose last cdr is nil. The Lisp printer knows the convention and doesn't print a dot in that case.
;; a cons cell
CL-USER> (cons 'b 'c)
(B . C)
[o|o]--- c
|
b
;; two cons cells, ending with a symbol: a dot.
CL-USER> (cons 'b (cons 'c 'd))
(B C . D)
;; several cons cells, ending with a symbol: still a dot (at the last cons cell):
CL-USER> (cons 'b (cons 'c (cons 'd (cons 'e 'f))))
(B C D E . F)
;; two cons cells, ending with nil: no dot.
CL-USER> (cons 'b (cons 'c nil))
(B C) ;; and not (B C . NIL)
[o|o]---[o|/]
| |
b c
;; with the list constructor:
CL-USER> (list 'b 'c)
(B C)
https://lispcookbook.github.io/cl-cookbook/data-structures.html#building-lists-cons-cells-lists

Related

How would I return a list that takes two lists and associates them with the contents in that list together in plait language for Racket?

I'm working on a problem that has me using Racket with plait language and I'm trying to get a program that takes two lists and associate them together like this. I am relatively new to Racket and the plait language.
'(a b c d) and '(1 2 3 4)
Together they should output:
'((make-assoc 'a 1) (make-assoc 'b 2) (make-assoc 'c 3) (make-assoc 'd 4)))
This is what I have defined so far:
#lang plait
(define-type Associate
(assoc [name : Symbol]
[values : Number]))
(define (make-assoc [names : (Listof Symbol)] [values : (Listof Number)]): (Listof assoc)
(map (lambda (name value) (assoc name value)) names values))
(test (make-assoc '(a b c d) '(1 2 3 4))
'((make-assoc 'a 1) (make-assoc 'b 2) (make-assoc 'c 3) (make-assoc 'd 4)))
(test (make-assoc '(t a c o tuesday) '(0 1 34 1729 42))
'((make-assoc 't 0) (make-assoc 'a 1) (make-assoc 'c 34) (make-assoc 'o 1729) (make-assoc 'tuesday 42)))
I tried to get somewhere coming up stuff for make-assoc but I am having trouble with syntax and I guess I can't use map or lambda because make-assoc is already the identifier. So maybe I could use append in some way?
First of all, the type is Associate, not assoc.
assoc is the name of its only variant, and is used to construct values of the Associate type.
> assoc
- (Symbol Number -> Associate)
#<procedure:assoc>
> (assoc 'hello 1234)
- Associate
(assoc 'hello 1234)
So you should have
(define (make-associate [names : (Listof Symbol)] [values : (Listof Number)]): (Listof Associate)
...
(You also need to rename it, as make-assoc is already taken by the "machinery".)
Second, the type of map is
(('a -> 'b) (Listof 'a) -> (Listof 'b))
so it takes a one-argument function and one list and produces a list.
You want map2, which has the type
(('a 'b -> 'c) (Listof 'a) (Listof 'b) -> (Listof 'c))
So,
(define (make-associate [names : (Listof Symbol)] [values : (Listof Number)]): (Listof Associate)
(map2 (lambda (name value) (assoc name value)) names values))
Third, quoting "suspends" evaluation; '(a b) is not the same as (list a b) – the former is a list of the symbols a and b while the latter is a list of the values those symbols are bound to.
> (define a 1)
> a
- Number
1
> 'a
- Symbol
'a
> (list a)
- (Listof Number)
'(1)
> '(a)
- (Listof Symbol)
'(a)
So you want to use list rather than ' in your test cases.
You also use assoc, not make-assoc, to create values of the Associate type.
In summary:
(test (make-associate '(a b c d) '(1 2 3 4))
(list (assoc 'a 1)
(assoc 'b 2)
(assoc 'c 3)
(assoc 'd 4)))
(test (make-associate '(t a c o tuesday) '(0 1 34 1729 42))
(list (assoc 't 0)
(assoc 'a 1)
(assoc 'c 34)
(assoc 'o 1729)
(assoc 'tuesday 42)))
And now,
good (make-associate '(a b c d) '(1 2 3 4)) at line 10
expected: (list (assoc 'a 1) (assoc 'b 2) (assoc 'c 3) (assoc 'd 4))
given: (list (assoc 'a 1) (assoc 'b 2) (assoc 'c 3) (assoc 'd 4))
good (make-associate '(t a c o tuesday) '(0 1 34 1729 42)) at line 14
expected: (list (assoc 't 0) (assoc 'a 1) (assoc 'c 34) (assoc 'o 1729) (assoc 'tuesday 42))
given: (list (assoc 't 0) (assoc 'a 1) (assoc 'c 34) (assoc 'o 1729) (assoc 'tuesday 42))

How does "Cons" work in Lisp?

I was studying Lisp and I am not experienced in Lisp programming. In a part of my studies I encountered the below examples:
> (cons ‘a ‘(a b)) ----> (A A B)
> (cons ‘(a b) ‘a) ----> ((A B).A)
I was wondering why when we have (cons ‘a ‘(a b)) the response is (A A B) and why when we change it a little and put the 'a after (a b), the response is a dotted list like ((A B).A)? What is the difference between the first code line and the second one? What is going on behind these codes?
It's pretty easy to understand if you think of them as cons-cells.
In short, a cons cell consists of exactly two values. The normal notation for this is to use the dot, e.g.:
(cons 'a 'b) ==> (A . B)
But since lists are used so often in LISP, a better notation is to drop the dot.
Lists are made by having the second element be a new cons cell, with the last ending a terminator (usually nil, or '() in Common Lisp). So these two are equal:
(cons 'a (cons 'b '())) ==> (A B)
(list 'a 'b) ==> (A B)
So (cons 'a 'b) creates a cell [a,b], and (list 'a 'b) will create [a, [b, nil]]. Notice the convention for encoding lists in cons cells: They terminate with an inner nil.
Now, if you cons 'a onto the last list, you create a new cons cell containing [[a, [b, nil]], a]. As this is not a "proper" list, i.e. it's not terminated with a nil, the way to write it out is to use the dot: (cons '(a b) 'a) ==> ((a b) . a).
If the dot wasn't printed, it would have to have been a list with the structure [[a, [b, nil]], [a, nil]].
Your example
When you do (cons 'a '(a b)) it will take the symbol 'a and the list '(a b) and put them in a new cons cell. So this will consist of [a, [a, [b, nil]]]. Since this naturally ends with an inner nil, it's written without dots.
As for (cons '(a b) 'a), now you'll get [[a, [b, nil]], a]. This does not terminate with an inner nil, and therefore the dot notation will be used.
Can we use cons to make the last example end with an inner nil? Yes, if we do
(cons '(a b) (cons 'a '())) ==> ((A B) A)
And, finally,
(list '(a b) 'a))
is equivalent to
(cons (cons (cons 'a (cons 'b '())) (cons 'a '())))
See this visualization:
CL-USER 7 > (sdraw:sdraw '(A A B))
[*|*]--->[*|*]--->[*|*]--->NIL
| | |
v v v
A A B
CL-USER 8 > (sdraw:sdraw '((A B) . A))
[*|*]--->A
|
v
[*|*]--->[*|*]--->NIL
| |
v v
A B
Also:
CL-USER 9 > (sdraw:sdraw '(A B))
[*|*]--->[*|*]--->NIL
| |
v v
A B
CL-USER 10 > (sdraw:sdraw (cons 'A '(A B)))
[*|*]--->[*|*]--->[*|*]--->NIL
| | |
v v v
A A B
CL-USER 11 > (sdraw:sdraw (cons '(A B) 'A))
[*|*]--->A
|
v
[*|*]--->[*|*]--->NIL
| |
v v
A B
A cons is a data structure that can contain two values. Eg (cons 1 2) ; ==> (1 . 2). The first part is car, the second is cdr. A cons is a list if it's cdr is either nil or a list. Thus
(1 . (2 . (3 . ()))) is a list.
When printing cons the dot is omitted when the cdr is a cons or nil. The outer parentheses of the cdr is also omitted. Thus (3 . ()) is printed (3) and (1 . (2 . (3 . ()))) is printed (1 2 3). It's the same structure, but with different visualization. A cons in the car does not have this rule.
The read function reads cons with dot and the strange exceptional print format when the cdr is a list. It will at read time behave as if it were cons.
With a special rule for both read and print the illusion of a list is complete even when it's chains of cons.
(cons ‘a ‘(a b)) ----> (A . (A B))
(cons ‘(a b) ‘a) ----> ((A B) . A)
When printing, the first is one list of 3 elements since the cdr is a list.
A list (a b c) is represented (stored internally) as three cons-cells: (cons 'a (cons 'b (cons 'c '()). Note that the last pair has '() in its cdr.
Series of cons-cells whose last cdr is '() is printed as a list by the printer. The example is thus printed as (a b c).
Let's look at: (cons 'a '(a b)).
The list '(a b) is represented as (cons 'a (cons 'b '()). This means that
(cons 'a '(a b)) produces: (cons 'a (cons 'a (cons 'b '())).
Let's look at: (cons '(a b) 'a).
The list '(a b) is represented as (cons 'a (cons 'b '()). This means that
(cons (cons '(a b) 'a)) produces (cons (cons 'a (cons 'b '()) 'a).
Note that this series does not end in '(). To show that the printer uses dot notation. ( ... . 'a) means that a value consists of a series of cons-cells and that the last cdr contains 'a. The value (cons (cons 'a (cons 'b '()) 'a) is thus printed as '((a b) . a).
cons is just a pair data type. For example, (cons 1 2) is a pair of 1 and 2, and it will be printed with the two elements seperated by a dot like (1 . 2).
Lists are internally represented as nested conses, e.g. the list (1 2 3) is (cons 1 (cons 2 (cons 3 '())).

Apply and keyword arguments in lisp

I can use keyword parameters in LISP
(member 'a '(a b c) :test #'eq)
However, when I tried to use apply to invoke member method
(apply #'member 'a '(a b c) :test #'eq)
I have error message as follows:
MEMBER: keyword arguments in (:TEST) should occur pairwise
[Condition of type SYSTEM::SIMPLE-PROGRAM-ERROR]
The solution was
(apply #'member 'a '(a b c) '(:test eq))
Whereas without the keyword arguments
(apply #'member 'a '((a b c)))
What's the logic behind this? Why '(:test #'eq) raises an error?
ADDED
This is the reason why I asked this question.
I have code from ANSI Common Lispbook page 103.
(defun our-adjoin (obj lst &rest args)
(if (apply #'member obj lst args)
lst
(cons obj lst)))
When I tried (our-adjoin 'a '(a b c)) it returns the result (A B C), but the our-adjoin can't be translated as (apply #'member 'a '(a b c)), because it will raise an error (as is asked in Apply and keyword arguments in lisp).
What I can think about is that the value from &rest args is given to make something like (apply #member 'a '(a b c) '()) not to raise an error.
apply expects its final argument to be a list. That is,
(apply #'foo (list 1 2 3)) == (foo 1 2 3)
(apply #'member 'a '(a b c) :test #'eq) == ??? ; is an error - #'eq isn't a list
I don't know what apply is making of #'eq (a function) where a list is expected, but that's the problem.
You might be looking for funcall instead of apply:
(funcall #'foo 1 2 3) == (foo 1 2 3)
(funcall #'member 'a '(a b c) :test #'eq) == (member 'a '(a b c) :test #'eq)
Edit: (apply #'member 'a '(a b c))
This is the same as
(member 'a 'a 'b 'c)
which of course is nonsense. Think of apply as "expanding" its last argument.
Edit 2: The our-adjoin code
(our-adjoin 'a '(a b c) :test #'eq)
;; is equivalent to
(if (apply #'member 'a '(a b c) (list :test #'eq))
lst
(cons obj lst))
;; is equivalent to
(if (member 'a '(a b c) :test #'eq) ...)
(our-adjoin 'a '(a b c))
;; is equivalent to
(if (apply #'member 'a '(a b c) (list)) ...) ; (list) == nil
;; is equivalent to
(if (member 'a '(a b c)) ...)
So your hypothesis (that the equivalent was (apply #'member 'a '(a b c) '())) is correct. (FYI, there is no difference between nil, 'nil, (), '(), and (list).)
APPLY is provided so that we can call functions with a computed argument list.
APPLY has the following syntax:
apply function &rest args+ => result*
the first parameter is a function
then several arguments, at least one, where the last argument needs to be a list

How to quote all items after comma-at them from a list

I want to expand (foo a b c d e ...) to ===> (bar 'a 'b 'c 'd 'e ...)
So far I only get this solution:
(defmacro foo (a1 &rest a2)
`(bar ',a1 '(,#a2)))
But it results in:
(foo a b c d) ===> (bar 'a '(b c d))
which is not what I want.
Does anyone have any idea?
'whatever is shorthand for (quote whatever). If you have a list of symbols like A, B, C, D, etc, and you want a list that contains the structure (bar (quote a) (quote b) (quote c) ...), you can do something like this:
`(bar ,#(mapcar (lambda (symbol) (list 'quote symbol)) symbols))

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