in scheme what is the meaning of dot not on cons? why sometimes it is valid and sometime not - 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?

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)

Related

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

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

Behavior of "unquote" when used as the second to last symbol in a quasiquoted proper list

I am trying to produce the list (1 unquote 2) using quasiquote. I have tried this:
`(1 unquote 2)
However, in Racket, MIT Scheme and Chez Scheme, I get a dotted list: '(1 . 2).
So I tried this:
`(1 'unquote 2)
However, I get (1 'unquote 2).
I finally got the list I wanted using this technique:
`(1 unquote 2 ,((lambda (x) x) 'unquote) 2) ; Returns: '(1 unquote 2)
Why do I get a dotted list out of a quasiquoted proper list when unquote is the second to last element in the quasiquoted list?
Actually, it does not always produce a dotted list. For example:
`(1 unquote (list 2 3 4)) ; Returns: '(1 2 3 4)
Please explain this strange behavior of unquote when it is the second to last element in a quasiquoted list.
(a b c) is a shorthand for (a . (b . (c . ()))).
So (quasiquote (1 unquote 2)) is really (quasiquote (1 . (unquote 2))) which is '(1 . 2).
(Or if you want to fully expand it, (quasiquote (1 unquote 2)) is (quasiquote . ((1 . (unquote . (2 . ()))) . ())))
Similarly, (quasiquote (1 unquote (list 2 3 4))) is really (quasiquote (1 . (unquote (list 2 3 4)))) = (quasiquote (1 . (2 3 4))) = '(1 2 3 4).
By the way, an easier way to produce '(1 unquote 2) using quasiquote is:
`(1 ,'unquote 2)
According to R5RS,
Unpredictable behavior can result if any of the symbols quasiquote,
unquote, or unquote-splicing appear in positions within a <qq
template> otherwise than as described above.
And your positions are not "as described above" - the form must be (unquote expression ...).
It has been upgraded to a syntax violation in R6RS.
[writing an answer rather than a comment because it's impossible to format backticks properly in a comment. Lisp/Scheme still handles this better than markdown ;-) ]
As others have explained (esp. Sorawee), unquote (aka ,) is a special name that is treated specially when inside an quasiquoted list (aka the backtick). To prevent its special meaning, a simple workaround is to make sure the special name doesn't appear textually.
Note that unquote does not have a special meaning inside quote (by contrast to inside quasiquote).
Thus this will work in all variants of Scheme:
(define unquote-sym 'unquote) ; `unquote` is inside `quote`, so it's a mere symbol here
`(1 ,unquote-sym 2) ; '(1 unquote 2)

semantic list in scheme

I am currently working on a implementing a standard semantic network and am beyond lost.
I have a global assc list:
(define *database* '())
and am trying to populate the list with the standard form as
((hellipcopter (isa (air-vehicle))
(has-part (propeller door)))
.
.
.
From The statements
(has-part helicopter propeller)
(has-part helicopter door)
(isa helicopter air-vehicle)
Here is my attempt
(define (process-relation rel)
(set! *database* (cons (cons (cons (cadr rel) (car rel)) (caddr rel))*database*)))
which prints out in this terrible fashion
((helicopter . has-part) . propeller)
((propeller . has-part) . blade)
I am new to scheme as you can tell, so i have some questions.
Why does cons form the statement like it does? (x . y)
How would you add a multiple parts, for instance, to the same object?
What would be the steps to fix this?
A list is a chain of cons. eg. if you evaluate '(1 . (2 . (3 . ()))) you get (1 2 3) since while the reader reads both dotted form and list form, the printer prints list form where it can. eg. if the cdr is a pair or () it omits the . and one pair of parentheses. If the last element in the chain is not () it is not a proper list and would require the dot form even for print. eg. '(1 . (2 . 3)) is (1 2 . 3).
(list 1 2 3) is the same as (cons 1 (cons 2 (cons 3 '())))
To fix you code some places you use cons you shoud use list or add more cons so that the values are only in the car position.

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

Cons in scheme explanation

(cons 1 2) gives us (1 . 2).
(cons 3 4) gives us (3 . 4).
So why does (cons (cons 1 2) (cons 3 4)) give us ((1 . 2) 3 . 4)?
Why isn't it ((1 . 2) (3 . 4))?
Well, it wouldn't be ((1 . 2) (3 . 4)) because that would be a list containing two elements, each a cons pair. I'm guessing what you meant was: why isn't it ((1 . 2) . (3 . 4))?
Well, actually the following two expressions are equivalent:
'((1 . 2) . (3 . 4))
'((1 . 2) 3 . 4)
This has to do with how Scheme's dotted notation works in tandem with its representation of proper lists. Remember that this:
'(1 . (2 . (3 . (4 . ()))))
...would simply be printed as this:
(1 2 3 4)
However, this:
'(1 . (2 . (3 . 4)))
...would be printed like this:
(1 2 3 . 4)
Note that Scheme tries to use the simplified list notation as long as it can—it only falls back to explicitly dotting things once it reaches a pair which doesn't have a pair or the empty list as its cdr element.
Therefore, in your original example, the second element of that cons pair is a pair, so Scheme uses the list notation. That lets it drop the second set of parentheses and the extra dot, yielding the result you encountered.

Resources