Reputation: 10920
Today, I made a typing mistake in the REPL and discovered a strange behaviour. Here's a sample of the interaction:
Welcome to Racket v6.11.
> (define x 3)
> x
3
> x'
3
> x
'x
>
So the first x
I typed resulted in 3
, which is expected. The x'
I accidentally typed resulted in 3
, which is unexpected. The last x
resulted in 'x
(!!).
It seems like there is something I don't understand about how the REPL reads values. Could someone explain why the REPL behaves in this way?
Upvotes: 2
Views: 56
Reputation: 8373
See Racketrivia: Using '
as an "identifier suffix" on the racket mailing list. The reply by Robby Findler and the reply by Matthias Felleisen both explain this.
The first line, x
, is normal.
The second line, x'
, is actually an "expression and-a-half." It's interpreted as an expression x
followed by an unfinished expression '
. The unfinished expression is allowed to be finished on the next line. Whatever you put on the next line will be put as the second half of ' next-line
.
That means the third x
is actually interpreted as the second half of ' x
.
You can see a better example of unfinished expressions with parentheses:
> 1 (list 2 ; 1 followed by an unfinished expression
1
> 3 4) ; finishing it
'(2 3 4)
> 3 4) ; on its own without the `(list 2` before it it's an error
3
4
; readline-input:13:3: read-syntax: unexpected `)` [,bt for context]
The quote after an expression is interpreted in a similar way:
> 1 ' ; 1 followed by an unfinished expression
1
> (indefatigable inexhaustible) ; finishes previously unfinished, put after a quote
'(indefatigable inexhaustible)
> (indefatigable inexhaustible) ; on its own without the quote before it it's an error
; indefatigable: undefined;
; cannot reference an identifier before its definition
; in module: top-level
; internal name: indefatigable
Upvotes: 2