using do in equal on scheme - why sometimes it is valid and sometime not - scheme

this is not duplicate of about the dot "." in scheme
what is the meaning of dot, other than just cons notation?
dot as I know is just cons notation. so I don't understand the meaning here:
why:
> (equal? . 8)
Exception: invalid syntax (equal? . 8)
Type (debug) to enter the debugger.
but:
> (equal? . ((quote . ((a . (b . (c . ()))))) . ('(a b c))))
#t
what is the meaning of the dot here?

In lisp languages, when the reader encounters something like (non-list1 . non-list2) it returns it as a “cons” cell, whose “car” is non-list1, and its “cdr” is non-list2.
Let's now consider the syntax (non-list . list). In this case the result of reading this s-expression can be seen again as a “cons” cell with non-list as “car”, and with list as “cdr”, but this is exactly the definition of a list which has its first element non-list, and the rest of its elements list.
So, for instance, (2 . ()) is read as (2), which is list that has 2 has its first element, and no other elements (so the rest of the list is the empty list, ()). Analogously, (1 . (2 . ())) is read as (1 2), and so on.
So, in your example, the syntax (equal? . 8) is returned by the reader as a cons cell with two atoms, a symbol and a number, and this is not a valid expression to be evaluated (remember that in lisp languages one can evaluate only lists with the first element the “operator” (function, macro, etc.), and the rest of the list as its arguments).
Let's now consider the second expression, and try to see if it is a list which is a valid expression to evaluate.
(equal? . ((quote . ((a . (b . (c . ()))))) . ('(a b c))))
The part (a . (b . (c . ()))) is simply the list (a b c). So, we have now:
(equal? . ((quote . ((a b c))) . ('(a b c))))
Remember that the syntax 'something is equivalent to (quote something) and vice-versa, we have now:
(equal? . ((quote (a b c)) . ('(a b c))))
and then:
(equal? . ('(a b c) . ('(a b c))))
let's interpret the rightmost dot:
(equal? . ('(a b c) '(a b c)))
and finally:
(equal? '(a b c) '(a b c))
which is a valid expression to evaluate. And remembering that 'X is the datum X, here we are comparing two lists (a b c) that are equal according to the predicate equal?.

Related

What is the concept of "third" in Primitive function scheme?

I am new at scheme computation.
I have a given problem:
(DEFINE (third C) (CAR(CDR CDR(C))))
(third ‘(A (B C) (D E) F))
I know the concept of CDR and CAR, but I don't understand what will "third" do. I read that it is a way to define "threesome???".
I am inclined to replace the value of C by the value of the second statement next to third which is ‘(A (B C) (D E) F), is this correct?
Can you explain in simple term as possible what "third" do in this given and how can I solve this given problem?
In lisps the list is a fundamental data structure, and it is comprised of pairs. Traditionally, the first member of the pair is called its car, and the second member is called its cdr:
( car . cdr )
Here the dot indicates that the pair is composed of two cells. Given a pair, (a . b), the accessor for the first member is also called car, and the accessor for the second member is called cdr. So:
(car '(a . b)) --> a, and
(cdr '(a . b)) --> b.
To form a list, pairs are combined in the following way: the first member of the first pair is the first element of the list, and the second member of the first pair is either the empty list or a pair representing the rest of the list. So, a list of one element, e.g., (a) is represented by the pair (a . ()).
A list of two elements, e.g., (a b) is represented by the pair (a . (b . ())). Here, the second member of the first pair is the pair (b . ()). You will note that the cdr of the list (a b) is the list (b), or equivalently (b . ()).
A list of three elements, e.g., (a b c) is similarly represented as (a . (b . (c . ()))). A list is a pair which has either the empty list () or a pair in its cdr. There is a distinction to be made here about proper lists (the final pair must have a () in its cdr) and improper lists, but I will ignore that distinction here. And the empty list is also a list (but not a pair). To make things a bit more precise: in Scheme a list is either the empty list or a pair whose cdr is a list.
So, car gets the first member of a list, and cdr gets the rest of the list. Given the list (a b c d) we can see that:
(cdr '(a b c d)) --> (b c d), and
(cdr (cdr '(a b c d))) --> (cdr '(b c d)) --> (c d), and
(car (cdr (cdr '(a b c d)))) --> (car (cdr '(b c d))) --> (car '(c d)) --> c.
So, given the definition:
(define (third xs)
(car (cdr (cdr xs))))
We have:
> (third '(a b c d))
c
I will leave it to the OP to apply this information to the question of:
(third '(a (b c) (d e) f)) --> ?

Can one use cons to do ((a . b) . (c . d)) and if not any other means or dot pair cannot have 2nd element like this?

;;; <- can one use cons to do ((a . b) . (c . d))?
(define x (cons a b)); nil -- should it be error
(define x (cons 'a 'b)); (a . b)
(define y (cons 'c 'd)); (c . d)
(define z00 (cons x y)) ; (((a . b) c . d) <- cannot use cons to do ((a . b) . (c . d))?
(define z01 (cons x 'y)) ; ((a . b) . y)
(define z10 (cons 'x y)) ; (x c . d)
(define z11 (cons 'x 'y)); (x . y))
(define z (list x y z00 z01 z10 z11))
; ((a . b) (c . d) ((a . b) c . d) ((a . b) . y) (x c . d) (x . y))
;;; and if not any other means or dot pair cannot have 2nd element like this?
Yes, you can. And the language has a wonderful predicate called equal? which will allow you to test this:
> (equal? (cons (cons 'a 'b) (cons 'c 'd))
'((a . b) . (c . d)))
#t
> (equal? '((a . b) . (c . d))
'((a . b) c . d))
#t
And you can even write a little display function which will confirm this:
(define (display-thing thing)
(if (cons? thing)
(begin
(display "(")
(display-thing (car thing))
(display " . ")
(display-thing (cdr thing))
(display ")"))
(display thing)))
And now
> (display-thing (cons (cons 'a 'b) (cons 'c 'd)))
((a . b) . (c . d))
> (display-thing '((a . b) . (c . d)))
((a . b) . (c . d))
> (display-thing '((a . b) c . d))
((a . b) . (c . d))
What this should all be telling you is that ((a . b) . (c . d)) and ((a . b) c . d) are merely different ways of writing a structurally identical object.
Pairs visualize differently based on their content. If the cdr of a pair contains the empty list it is a proper list an dthe dot and extra empty list is not shown:
(cons 'a '())
'(a . ())
; ==> (a)
A pair that has pair as it's cdr can be visualized as a list element without the . and extra parenthesis:
(cons 'b '(a))
'(b . (a))
; ==> (b a)
(cons 'b '(a . c))
'(b . (a . c))
; ==> (b a . c)
These are just made so that we can have (1 2 3) displayed instead of (1 . (2 . (3 . ()))) which is how it really is made.
If you were to not have a pair or a empty list in the cdr then it falls back to showing the dotted pair:
(cons 'a 'b)
'(a . b)
; ==> (a . b)
In your example '((a . b) . (c . d)) because there is a pair after a dot (eg. the cdr if the pair the visualization will remove the dot and one pair of parentheses and show it like ((a . b) c . d). This is the only acceptable correct way for a REPL to display this even though both your example and the display will be read in as the same structure.
There is a similar issue with numbers. In code you can use 10, #xa and #o12 to get the number 10 and the value will have no idea what format is was read in as and only show the base 10 in the REPL.
;;; ```
;;; <- can one use cons to do ((a . b) . (c . d))?
(define x (cons a b)); nil -- should it be error
(define x (cons 'a 'b)); (a . b)
(define y (cons 'c 'd)); (c . d)
(define z00 (cons x y)) ; (((a . b) c . d) <- cannot use cons to do ((a . b) . (c . d))?
(define z01 (cons x 'y)) ; ((a . b) . y)
(define z10 (cons 'x y)) ; (x c . d)
(define z11 (cons 'x 'y)); (x . y))
(define z22 (cons '(f g) '(h i)))
(define z2c (cons (cons 'f 'g) (cons 'h 'i)))
(define fgc (cons ('f 'g))); should it be error (nil)
; actually not as 'f is an exoression (quote f) and f for some reason is nil it becomes nil from quote of nil abd so is the second one. now (nil . nil) is (nil)
(define z (list x y z00 z01 z10 z11 z22 z2c fgc))
; ((a . b) (c . d) ((a . b) c . d) ((a . b) . y) (x c . d) (x . y))
;;; ```
;;; and if not any other means or dot pair cannot have 2nd element like this?
;;; possibly not
;;; as the cons join 2 pairs of dotted pairs and can generate one dotted pair
;;; ((a . b) . (c . d)) but the printing rule is reflected the list bias
;;; this new dotted pair will have the first element as (( a . b) ... print as
;;; ((a . b) ...
;;; the 2nd element it will consider whether it is an atom or another dotted pair
;;; (other possibilities like loop back or something else ... not sure)
;;; as (c . d) is a dotted pair the "printing" continues as a list would
;;;
;;; ((a . b) c ...
;;; however the second element of (c . d) is not a dotted pair but an atom and print as
;;; . d) will it becomes
;;; hence even though you form the binary tree head the dot pair would display as a partial list
;;; you can have a list of dotted pairs like z
;;; but not dotted pair of dotted pairs

in scheme what is the meaning of dot not on cons? why sometimes it is valid and sometime not

what is the meaning of dot, other than just cons notation?
dot as I know is just cons notation. so I don't understand the meaning here:
why:
> (equal? . 8)
Exception: invalid syntax (equal? . 8)
Type (debug) to enter the debugger.
but:
> (equal? . ((quote . ((a . (b . (c . ()))))) . ('(a b c))))
#t
what is the meaning of the dot here?
The syntax gets parsed and for a Scheme reader '(1 . (2 . ())) gets parsed and creates the same structure as '(1 2). Basically proper lists are singly linked lists where the last cdr points to the empty list.
(somefun . (5)) is the same as (somefun 5) for the reader. When the source code is turned in to a data structure, the interpreter won't know if you wrote the one or the other. Hovever (somefun . 5) doesn't get parsed to a function with one argument since the argument list itself is a number. the parser parsed it into a data structure, but your scheme implementation responsible to compile it or interpret it failed. You may use dotted list in data and some syntax like lambda for rest arguments, but it is very limited. Most of the time using dotted pairs in code fails.
A Scheme system will display structure only one way. eg. if you put in '(1 . (2 . ())) you will se (1 2) back since a dot with a list or empty list cdr gets special list visualization. If you do '(1 . (2 . 3)) it can start using the special rule since the cdr is a pair, but the next won't fly ending it up as (1 2 . 3). If you evaluate '(equal? . ((quote . ((a . (b . (c . ()))))) . ('(a b c)))) you get (equal? '(a b c) '(a b c)) back and that is because it is the simplest visualization of that structure. As code they read to the same structure and evaluate the same.
The most common problem when learning Scheme is to understand the list structure. Eg. how to create ((1 2) (3 4)) using cons and how to access the 4. If you knew it is really ((1 . (2 . ()) . ((3 . (4 . ())))) you see the 4 is after fllowing the cdr, car, cdr, then car or the simplified (cadadr '((1 2) (3 4))). The visualizer will just omit the dot and the extra set of parentheses in the cdr if it has them. eg. '((a) . (b)) will become ((a) b)

Dotted lists in Guile Scheme

How can I specifically check for dotted-lists in the form (a . b) Guile? The dotted-list of srfi-1 strangely returns #t also for e.g. Numbers (since when are numbers Lists too? https://www.gnu.org/software/guile/manual/html_node/SRFI_002d1-Predicates.html)! And pair? will evaluate to #t also for normal lists. Is there a way to differentiate the (a . b) construct from other things, whereas the b part (the cdr) could itself be any object including other association lists etc.?
This is what i didn't expect and can't understand:
(dotted-list? '(c . ((d . 3)
(e . 4)))) ; ===> #f
(dotted-list? 3) ; ===> #t
Note that (atom . (x1 ... xn)) is another way of writing (atom x1 ... xn), so (c . ((d . 3) (e . 4))) is just equivalent to (c (d . 3) (e . 4)) which is nothing more than a three element list (and for this reason dotted-list? returns false in this case).
If you don't like the definition of dotted-list? given in srf-1 then define your own version:
(define (my-dotted-list? l)
(and (dotted-list? l)
(pair? l)))
If you wanted a standalone definition (one that doesn't depend on an existing definition which you don't agree with), then I think this is reasonable:
(define (dotted-list? c)
(and (cons? c)
(cond [(cons? (cdr c))
(dotted-list? (cdr c))]
[(null? (cdr c))
#f]
[else #t])))
Note that like any simple-minded definition this fails to halt on circular lists.

Equivalents with less few dots in scheme language?

what are the equivalent structures with few dots for these in scheme language
’((((a . b) . c) . d) . ())
’((a . ( b . ())) . ())
'(a . ( b . c ))
’(a . (( b . c) . ()))
for example : for '(a . (b . (c . (d . ())))) the equivalent is (a b c d e)
It's as simple as evaluating the expressions in the interpreter and observing what gets printed:
'((((a . b) . c) . d))
'((a b))
'(a b . c)
'(a (b . c))
Remember: the dot notation is just a convention, it's shown when a list structure is improper (that is, it doesn't end with an empty list). That's why this is printed exactly the same:
'(a . b)
=> '(a . b)
But this is printed without the dot, it's implied that the last element is an empty list, so there's no need to show it:
'(a b . ())
=> '(a b)
Read the documentation to learn more about all the quirks of the dot notation, in particular pay attention to this section:
In general, the rule for printing a pair is as follows: use the dot notation always, but if the dot is immediately followed by an open parenthesis, then remove the dot, the open parenthesis, and the matching close parenthesis. Thus, (0 . (1 . 2)) becomes (0 1 . 2), and (1 . (2 . (3 . ()))) becomes (1 2 3).

Resources