What is ' (apostrophe) in Racket?
Asked Answered
T

2

4

I am a little confused about the meaning of the ' sign in racket. It appears to me that the same sign has different meanings. Look at 2 simple examples below:

list

Returns a newly allocated list containing the vs as its elements.

> (list 1 2 3 4)
'(1 2 3 4)

quote

Produces a constant value corresponding to datum (i.e., the representation of the program fragment) without its lexical information, source location, etc. Quoted pairs, vectors, and boxes are immutable.

> '(1 2 3 4)
'(1 2 3 4)

So my question is: Does the ' sign has 2 meanings (a symbol and a list) or are these the same data type and list actually returns a quoted constant value? If the second is the case why does this work:

> '(+ (- 2 13) 11)
'(+ (- 2 13) 11)

> (eval (list + (- 2 13) 11))
0

(also (eval '(+ (- 2 13) 11)) works and evaluates correctly to 0)

But this does not:

> (list + (- 2 13) 11)
'(#<procedure:+> -11 11)

> (eval '(#<procedure:+> -11 11))
. read: bad syntax `#<'

Related maybe: What is ' (apostrophe) in Lisp / Scheme?

Twilley answered 18/11, 2015 at 12:6 Comment(1)
The reason (eval '(#<procedure:+> -11 11)) doesn't work is that #<procedure:+> cannot be read back in. However, using quasiquote and unquote, (eval (quasiquote ((unquote +) -11 11))) works. Using the short hand that would be written as (eval `(,+ -11 11)).Aquifer
T
4

> is a sign of REPL - the Read-Eval-Print Loop.

First, whatever expression you've typed at the REPL prompt is read - converted to some internal abstract syntax tree representation. Then this internal representation of the typed-in expression is evaluated - i.e. its value is found. Then the result is printed.

When we type

> (list 1 2 3 4)

the typed-in expression is read as a nested structure, let's write it as

[LIST | [1 | [2 | [3 | [4 | NIL ]]]]]

according to the usual representation of lists as pairs of data and the rest of the list (here showing a pair of a and b as [a | b]).

Then the above structure is evaluated, and because its first element was LIST it causes the invocation of list with the arguments as specified, which causes a fresh list structure to be built, which can be represented as

[1 | [2 | [3 | [4 | NIL ]]]]

Then it is printed, usually as (1 2 3 4) but Racket chooses to print it as '(1 2 3 4). Incidentally, it can't be evaluated, because 1 can not be called.

Next, the quoted expression '(1 2 3 4), which is read as (quote (1 2 3 4)). It is converted into

[QUOTE | [ [1 | [2 | [3 | [4 | NIL ]]]] | NIL ]]

which, when evaluated (according to the evaluation rule for quote), returns the data it received. Which we represent as

[1 | [2 | [3 | [4 | NIL ]]]]

That's why the two are similar. Whether we build a new list containing 1, 2, 3, and 4; or we cause it to be created as part of read process, so it gets returned verbatim by quote; the result is the same.

Thiazole answered 18/11, 2015 at 16:46 Comment(2)
thank you for clarification. I think in combination with the previous answer I finally got it :)Twilley
you're most welcome. :) some claim this is the most confusing part of Scheme. Besides the call/cc of course. :)Thiazole
T
7

You are confused by the default way of #lang racket of printing values, which differs from almost all the other interactive lisp environments. If you choose in DrRacket itself another language, for instance R5RS, you will discover that it prints:

> (list 1 2 3 4)
(1 2 3 4)

that is, the result of the operator list applied to the numbers 1 2 3 4 is to produce a list of those numbers, which is exactly the interpretation of (1 2 3 4).

So, the answer to What is ' (apostrophe) in Lisp / Scheme? is valid also for your case, and 'anything is just an abbreviation for (quote anything).

Terpsichore answered 18/11, 2015 at 12:30 Comment(2)
ok. Thank you for the clarification. But why does (eval (list + (- 2 13) 11)) evaluate to 0? Is a function-call nothing more than a list of the function name first followed by its parameters? Because (eval (list 1 2 3 4)) results in the error 1 is not a procedure.Twilley
“Is a function-call nothing more than a list of the function name first followed by its parameters?” Almost correct but not completely. In the language what is evaluated is a form, that is special list where the first argument is the function. Your first example works since “+” is the name of a function, and the result of (list + (- 2 13) 11) is the list (<the function +> -11 11) (each argument of list is evaluated). In your second example the result of (list 1 2 3 4) is the list (1 2 3 4), which is not a form, since 1 is not a function.Terpsichore
T
4

> is a sign of REPL - the Read-Eval-Print Loop.

First, whatever expression you've typed at the REPL prompt is read - converted to some internal abstract syntax tree representation. Then this internal representation of the typed-in expression is evaluated - i.e. its value is found. Then the result is printed.

When we type

> (list 1 2 3 4)

the typed-in expression is read as a nested structure, let's write it as

[LIST | [1 | [2 | [3 | [4 | NIL ]]]]]

according to the usual representation of lists as pairs of data and the rest of the list (here showing a pair of a and b as [a | b]).

Then the above structure is evaluated, and because its first element was LIST it causes the invocation of list with the arguments as specified, which causes a fresh list structure to be built, which can be represented as

[1 | [2 | [3 | [4 | NIL ]]]]

Then it is printed, usually as (1 2 3 4) but Racket chooses to print it as '(1 2 3 4). Incidentally, it can't be evaluated, because 1 can not be called.

Next, the quoted expression '(1 2 3 4), which is read as (quote (1 2 3 4)). It is converted into

[QUOTE | [ [1 | [2 | [3 | [4 | NIL ]]]] | NIL ]]

which, when evaluated (according to the evaluation rule for quote), returns the data it received. Which we represent as

[1 | [2 | [3 | [4 | NIL ]]]]

That's why the two are similar. Whether we build a new list containing 1, 2, 3, and 4; or we cause it to be created as part of read process, so it gets returned verbatim by quote; the result is the same.

Thiazole answered 18/11, 2015 at 16:46 Comment(2)
thank you for clarification. I think in combination with the previous answer I finally got it :)Twilley
you're most welcome. :) some claim this is the most confusing part of Scheme. Besides the call/cc of course. :)Thiazole

© 2022 - 2024 — McMap. All rights reserved.