Why is this legal (rocket) scheme?
I was going through htdp and found this somewhere in the beginning: -
Explain why the following sentences are illegal definitions: 1. (define (f 'x) x)
However, it works fine in a racket:
> (define (f 'x) x)
> (f 'a)
3
> (define a 5)
> (f a)
3
Obviously I'm missing something ... what exactly?
Short answer: you shouldn't use the full "#lang racket" language. Languages ββof instruction eliminate the potentially confusing additional capabilities of the language you encounter.
In this case, your definition is interpreted as a function named f with an optional argument called quotation, which defaults to "x".
Set your language level to Beginner Learner and you will get a much smarter answer.
This line does not work for me in the Racket: (define (f 'x) x)
. An error is reported define: not an identifier for procedure argument in: (quote x)
.
What language are you using? have you tried to run the above line in the interaction window?