4

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 racket:

> (define (f 'x) x)
> (f 'a)
3
> (define a 5)
> (f a)
3

Obviously, I'm missing something ... what, exactly ?

soegaard
  • 30,661
  • 4
  • 57
  • 106
agam
  • 5,064
  • 6
  • 31
  • 37

2 Answers2

13

Short answer: you should not be using the full "#lang racket" language. The teaching languages strip out the potentially confusing advanced features of the language that you're encountering.

In this case, your definition is being interpreted as a function called f with an optional argument called quote whose default value is provided by 'x'.

Set the language level to Beginning Student, and you'll get a much more reasonable answer.

John Clements
  • 16,895
  • 3
  • 37
  • 52
  • 3
    I'm guessing he had an earlier definition of 'x' as three. – John Clements Mar 08 '12 at 04:30
  • 2
    Hence the motivation for the teaching languages in DrRacket. Full-on Racket has (mis)features that a beginner can trip up on. Pretty much the same story for any professional-strength language; see the very silly **Wat** presentation (https://www.destroyallsoftware.com/talks/wat) for concrete examples. :) – dyoo Mar 09 '12 at 03:12
  • 1
    @dyoo --- are you referring to my skepticism expressed in an earlier comment discussion? Because this immediately brought that discussion to mind, and made me think again. (On the other hand, is this behavior even intentional in full Racket?) – JasonFruit Mar 09 '12 at 16:25
  • 1
    @JasonFruit in re: "is this behavior intentional": that question is a bit open-ended, but I agree that the expansion of 'foo into (quote foo), which happens at the reader level---that is, before the syntax expander starts---has an unhygienic feel. You'd expect this quote conversion to be legal only in an "expression position"; the problem is that there's no notion of an "expression position" at read time, that I'm aware of. BTW, I'd be delighted for Eli to jump in here and tell me of how this could be useful. – John Clements Mar 09 '12 at 22:20
0

This line does not work for me in Racket: (define (f 'x) x). The error reported is define: not an identifier for procedure argument in: (quote x).

What language are you using? did you try to run the above line in the interaction window?

Óscar López
  • 232,561
  • 37
  • 312
  • 386