It’s not official so I’d prefer not to use the racket name. It might be a possibility if it evolves into a successful way of introducing beginners and promoting racket.
@vijkum0692 has joined the channel
@thongpv87 has joined the channel
I try to run this code from SICP book: (define (count-leaves x)
(cond ((null? x) 0)
((not (pair? x)) 1)
(else (+ (count-leaves (car x))
(count-leaves (cdr x))))))
(count-leaves '('(1 2) '(3 4)))
6
(count-leaves '((1 2) '(3 4)))
5
How can I explain the result
@thongpv87 The '
is turned into (quote ...)
by the reader. So '(1 2)
is short for (quote (1 2))
. This means that the symbol quote
is counted in the first example.
I thought the '
is the short cut to construct a list.
won’t it be turned into (list 1 2)
?
or actually (quote (1 2))
?
If I say “simon” you say simon.
The ’ or quote is the same concept for daturms.
A ’datum will produce a value that prints as datum.
so isn’t (1 2)
the datum here?
Racket says (quote 1 2)
is bad syntax
The concept is called quotation, but since (quote x) is cumbersome to write, you can abbreviate it as ’x.
(quote (1 2))
yeah, you just said above: > So '(1 2)
is short for (quote 1 2)
.
so I was briefly confused
Sorry - miswrote. Edited.
Quotation works with vectors and hashtables too.
is '(1 2)
equivalent to (list 1 2)
?
no
it’s equivalent to (quote (1 2))
although (list 1 2)
seems to evaluate to the same thing
So quote
is more abstract than list
since it can construct vector and hashtable ?
It is does a different job. If you get some result printed in the repl, then you can copy paste the result into a program and put a ’ before it.
Thank you for the explanation.
Why this give me the result 3
: (length (cons (list 1 2) (list 3 4)))
I thing the result should be 2.
cons isn’t append, so cons puts the entire (list 1 2) as the first element of a new list
so you get a list of (list 1 2), 3, and 4
so 3 items
Ah, I just forgot that. Thanks.