简体   繁体   中英

define quote process in racket?

I have this couple of lines that I'm running on drracket, I can't understand the output

> (define 'a 5)
> 'b
. . ..\..\Program Files\Racket\share\pkgs\drracket\drracket\private\rep.rkt:1088:24: b: undefined;
 cannot reference an identifier before its definition
> '0
5

is quote redefined? why 'b isn't working and '0 is 5?

First, symbols are atomic values. They cannot be treated like variables.

Anyway, your first line expands to:

(define (quote a) 5)

which is shorthand for defining functions in racket. So yes, you are redefining quote .

When you try to run 'b , you're running (quote b) , where it expects a variable b to have some value, which is does not. That's why you receive the error, cannot reference an identifier before its definition .

When you try to run '0 , you're running (quote 0) . 0 is a valid value, and it becomes the value for a in your new function. So, the function evaluates as normal, and returns 5.

In other words, it's not just 0 that is a valid argument.

> (define 'a 5)
> (define b 12345)
> 'b
5
> '0
5
> '123454321
5

Check out the Racket documentation on symbols . Symbols don't contain values; they are values. You'll want to use variables instead ( (define a 5) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM