Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65919841/using-do-in-equal-on-scheme-why-sometimes-it-is-valid-and-sometime-not

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...