0

I have been banging around on google and drRacket trying to understand what the apostrophe ' before a procedure means in racket and how I could remove it. What I'm trying to do is take a + from inside a list i.e. '(+ 1 2). However, every time I do something like (first x) (where x is the list in the example) I receive '+ instead of just + (notice the apostrophe). How can I remove the apostrophe and what is its purpose?

Osama Hafez
  • 315
  • 1
  • 4
  • 12

1 Answers1

3

The ' apostrophe, pronounced quote, mean that the stuff inside will be interpreted as data for an s-expression, not evaluated as code.

'x, 'hello, and '+ are all symbols, which are like strings with unique-identity properties. They contain only text, not "meaning", so the '+ symbol does not contain a reference to the + function.

If you use parentheses under a ' quote, it will create a list with all the elements also ' quoted. In other words, '(x y z) is equivalent to (list 'x 'y 'z). You can think of this as quote "distributing itself" over all the elements inside it.

In your case, '(+ 1 2) is equivalent to (list '+ '1 '2), which is the same as (list '+ 1 2) because numbers are already literal data.

In most cases, the best way to get rid of a ' quote is to not add one on the outside in the first place. Instead of '(+ 1 2) you could use list: (list + 1 2), or the more advanced forms ` quasiquote and , unquote: `(,+ 1 2). In either of these cases, the + never gets put under a quote in the first place. It never becomes a symbol like '+. The + outside of any quote has meaning as the addition function.

In other cases, you can't avoid having the symbol '+ because it comes from intrinsically textual data. In this case you can assign meaning to it with an interpreter. Somewhere in that interpreter you might want code like this

(match sym ['+ +] ['- -] ['* *] ['/ /] [_ (error "unrecognized symbol")])

Something is needed to assign meaning externally, because the symbol '+ does not have that meaning internally. You can either define the interpreter yourself or use an existing one such as eval, as long as all the meanings in the interpreter correspond exactly to what you intend.

Alex Knauth
  • 8,133
  • 2
  • 16
  • 31