Dereference variable inside list - scheme

Is it possible to dereference a variable contained inside a list, to obtain its value? For example:
(define one 1)
(define two 2)
(define list '(one two))
(display (list-ref list 0))
Here list-ref references to one, and display shows one in letters. Could instead one dereference to the value contained by the homonym variable?

Eval can definitely solve this problem... but like most situations where eval is applicable, it's a large and dangerous hammer.
Matthew Flatt's blog post on this topic has become the go-to explanation:
http://blog.racket-lang.org/2011/10/on-eval-in-dynamic-languages-generally.html
Here's how you might do it without eval in Racket. Stripping away the cruft, "dict-ref" can find a named
element (or elements) in an "association list".
#lang racket
(define data
'((one 1)
(two 2)))
(define wanted-list '(two one))
;; evaluates to '((2) (1)):
(for/list ([wanted wanted-list])
(dict-ref data wanted))

That's happening because the quoted list contains two symbols, one and two. Try the following:
(display (eval (list-ref list 0)))
To resolve that symbol.

Related

Scheme Language: +: contract violation, expected: number? [duplicate]

I know that you can use ' (aka quote) to create a list, and I use this all the time, like this:
> (car '(1 2 3))
1
But it doesn’t always work like I’d expect. For example, I tried to create a list of functions, like this, but it didn’t work:
> (define math-fns '(+ - * /))
> (map (lambda (fn) (fn 1)) math-fns)
application: not a procedure;
expected a procedure that can be applied to arguments
given: '+
When I use list, it works:
> (define math-fns (list + - * /))
> (map (lambda (fn) (fn 1)) math-fns)
'(1 -1 1 1)
Why? I thought ' was just a convenient shorthand, so why is the behavior different?
TL;DR: They are different; use list when in doubt.
A rule of thumb: use list whenever you want the arguments to be evaluated; quote “distributes” over its arguments, so '(+ 1 2) is like (list '+ '1 '2). You’ll end up with a symbol in your list, not a function.
An in-depth look at list and quote
In Scheme and Racket, quote and list are entirely different things, but since both of them can be used to produce lists, confusion is common and understandable. There is an incredibly important difference between them: list is a plain old function, while quote (even without the special ' syntax) is a special form. That is, list can be implemented in plain Scheme, but quote cannot be.
The list function
The list function is actually by far the simpler of the two, so let’s start there. It is a function that takes any number of arguments, and it collects the arguments into a list.
> (list 1 2 3)
(1 2 3)
This above example can be confusing because the result is printed as a quoteable s-expression, and it’s true, in this case, the two syntaxes are equivalent. But if we get slightly more complicated, you’ll see that it is different:
> (list 1 (+ 1 1) (+ 1 1 1))
(1 2 3)
> '(1 (+ 1 1) (+ 1 1 1))
(1 (+ 1 1) (+ 1 1 1))
What’s going on in the quote example? Well, we’ll discuss that in a moment, but first, take a look at list. It’s just an ordinary function, so it follows standard Scheme evaluation semantics: it evaluates each of its arguments before they get passed to the function. This means that expressions like (+ 1 1) will be reduced to 2 before they get collected into the list.
This behavior is also visible when supplying variables to the list function:
> (define x 42)
> (list x)
(42)
> '(x)
(x)
With list, the x gets evaluated before getting passed to list. With quote, things are more complicated.
Finally, because list is just a function, it can be used just like any other function, including in higher-order ways. For example, it can be passed to the map function, and it will work appropriately:
> (map list '(1 2 3) '(4 5 6))
((1 4) (2 5) (3 6))
The quote form
Quotation, unlike list, is a special part of Lisps. The quote form is special in part because it gets a special reader abbreviation, ', but it’s also special even without that. Unlike list, quote is not a function, and therefore it does not need to behave like one—it has rules of its own.
A brief discussion of Lisp source code
In Lisp, of which Scheme and Racket are derivatives, all code is actually made up of ordinary data structures. For example, consider the following expression:
(+ 1 2)
That expression is actually a list, and it has three elements:
the + symbol
the number 1
the number 2
All of these values are normal values that can be created by the programmer. It’s really easy to create the 1 value because it evaluates to itself: you just type 1. But symbols and lists are harder: by default, a symbol in the source code does a variable lookup! That is, symbols are not self-evaluating:
> 1
1
> a
a: undefined
cannot reference undefined identifier
As it turns out, though, symbols are basically just strings, and in fact we can convert between them:
> (string->symbol "a")
a
Lists do even more than symbols, because by default, a list in the source code calls a function! Doing (+ 1 2) looks at the first element in the list, the + symbol, looks up the function associated with it, and invokes it with the rest of the elements in the list.
Sometimes, though, you might want to disable this “special” behavior. You might want to just get the list or get the symbol without it being evaluated. To do this, you can use quote.
The meaning of quotation
With all this in mind, it’s pretty obvious what quote does: it just “turns off” the special evaluation behavior for the expression that it wraps. For example, consider quoteing a symbol:
> (quote a)
a
Similarly, consider quoteing a list:
> (quote (a b c))
(a b c)
No matter what you give quote, it will always, always spit it back out at you. No more, no less. That means if you give it a list, none of the subexpressions will be evaluated—do not expect them to be! If you need evaluation of any kind, use list.
Now, one might ask: what happens if you quote something other than a symbol or a list? Well, the answer is... nothing! You just get it back.
> (quote 1)
1
> (quote "abcd")
"abcd"
This makes sense, since quote still just spits out exactly what you give it. This is why “literals” like numbers and strings are sometimes called “self-quoting” in Lisp parlance.
One more thing: what happens if you quote an expression containing quote? That is, what if you “double quote”?
> (quote (quote 3))
'3
What happened there? Well, remember that ' is actually just a direct abbreviation for quote, so nothing special happened at all! In fact, if your Scheme has a way to disable the abbreviations when printing, it will look like this:
> (quote (quote 3))
(quote 3)
Don’t be fooled by quote being special: just like (quote (+ 1)), the result here is just a plain old list. In fact, we can get the first element out of the list: can you guess what it will be?
> (car (quote (quote 3)))
quote
If you guessed 3, you are wrong. Remember, quote disables all evaluation, and an expression containing a quote symbol is still just a plain list. Play with this in the REPL until you are comfortable with it.
> (quote (quote (quote 3)))
''3
(quote (1 2 (quote 3)))
(1 2 '3)
Quotation is incredibly simple, but it can come off as very complex because of how it tends to defy our understanding of the traditional evaluation model. In fact, it is confusing because of how simple it is: there are no special cases, there are no rules. It just returns exactly what you give it, precisely as stated (hence the name “quotation”).
Appendix A: Quasiquotation
So if quotation completely disables evaluation, what is it good for? Well, aside from making lists of strings, symbols, or numbers that are all known ahead of time, not much. Fortunately, the concept of quasiquotation provides a way to break out of the quotation and go back into ordinary evaluation.
The basics are super simple: instead of using quote, use quasiquote. Normally, this works exactly like quote in every way:
> (quasiquote 3)
3
> (quasiquote x)
x
> (quasiquote ((a b) (c d)))
((a b) (c d))
What makes quasiquote special is that is recognizes a special symbol, unquote. Wherever unquote appears in the list, then it is replaced by the arbitrary expression it contains:
> (quasiquote (1 2 (+ 1 2)))
(1 2 (+ 1 2))
> (quasiquote (1 2 (unquote (+ 1 2))))
(1 2 3)
This lets you use quasiquote to construct templates of sorts that have “holes” to be filled in with unquote. This means it’s possible to actually include the values of variables inside of quoted lists:
> (define x 42)
> (quasiquote (x is: (unquote x)))
(x is: 42)
Of course, using quasiquote and unquote is rather verbose, so they have abbreviations of their own, just like '. Specifically, quasiquote is ` (backtick) and unquote is , (comma). With those abbreviations, the above example is much more palatable.
> `(x is: ,x)
(x is: 42)
One final point: quasiquote actually can be implemented in Racket using a rather hairy macro, and it is. It expands to usages of list, cons, and of course, quote.
Appendix B: Implementing list and quote in Scheme
Implementing list is super simple because of how “rest argument” syntax works. This is all you need:
(define (list . args)
args)
That’s it!
In contrast, quote is a lot harder—in fact, it’s impossible! It would seem totally feasible, since the idea of disabling evaluation sounds a lot like macros. Yet a naïve attempt reveals the trouble:
(define fake-quote
(syntax-rules ()
((_ arg) arg)))
We just take arg and spit it back out... but this doesn’t work. Why not? Well, the result of our macro will be evaluated, so all is for naught. We might be able to expand to something sort of like quote by expanding to (list ...) and recursively quoting the elements, like this:
(define impostor-quote
(syntax-rules ()
((_ (a . b)) (cons (impostor-quote a) (impostor-quote b)))
((_ (e ...)) (list (impostor-quote e) ...))
((_ x) x)))
Unfortunately, though, without procedural macros, we can’t handle symbols without quote. We could get closer using syntax-case, but even then, we would only be emulating quote’s behavior, not replicating it.
Appendix C: Racket printing conventions
When trying the examples in this answer in Racket, you may find that they do not print as one would expect. Often, they may print with a leading ', such as in this example:
> (list 1 2 3)
'(1 2 3)
This is because Racket, by default, prints results as expressions when possible. That is, you should be able to type the result into the REPL and get the same value back. I personally find this behavior nice, but it can be confusing when trying to understand quotation, so if you want to turn it off, call (print-as-expression #f), or change the printing style to “write” in the DrRacket language menu.
The behavior you are seeing is a consequence of Scheme not treating symbols as functions.
The expression '(+ - * /) produces a value which is a list of symbols. That's simply because (+ - * /) is a list of symbols, and we are just quoting it to suppress evaluation in order to get that object literally as a value.
The expression (list + - * /) produces a list of functions. This is because it is a function call. The symbolic expressions list, +, -, * and / are evaluated. They are all variables which denote functions, and so are reduced to those functions. The list function is then called, and returns a list of those remaining four functions.
In ANSI Common Lisp, calling symbols as functions works:
[1]> (mapcar (lambda (f) (funcall f 1)) '(+ - * /))
(1 -1 1 1)
When a symbol is used where a function is expected, the top-level function binding of the symbol is substituted, if it has one, and everything is cool. In effect, symbols are function-callable objects in Common Lisp.
If you want to use list to produce a list of symbols, just like '(+ - * /), you have to quote them individually to suppress their evaluation:
(list '+ '- '* '/)
Back in the Scheme world, you will see that if you map over this, it will fail in the same way as the original quoted list. The reason is the same: trying to use a symbol objects as a functions.
The error message you are being shown is misleading:
expected a procedure that can be applied to arguments
given: '+
This '+ being shown here is (quote +). But that's not what the application was given; it was given just +, the issue being that the symbol object + isn't usable as a function in that dialect.
What's going on here is that the diagnostic message is printing the + symbol in "print as expression" mode, a feature of Racket, which is what I guess you're using.
In "print as expression" mode, objects are printed using a syntax which must be read and evaluated to produce a similar object. See the StackOverflow question "Why does the Racket interpreter write lists with an apostroph before?"

lisp, quote, substitution model of calculation [duplicate]

I know that you can use ' (aka quote) to create a list, and I use this all the time, like this:
> (car '(1 2 3))
1
But it doesn’t always work like I’d expect. For example, I tried to create a list of functions, like this, but it didn’t work:
> (define math-fns '(+ - * /))
> (map (lambda (fn) (fn 1)) math-fns)
application: not a procedure;
expected a procedure that can be applied to arguments
given: '+
When I use list, it works:
> (define math-fns (list + - * /))
> (map (lambda (fn) (fn 1)) math-fns)
'(1 -1 1 1)
Why? I thought ' was just a convenient shorthand, so why is the behavior different?
TL;DR: They are different; use list when in doubt.
A rule of thumb: use list whenever you want the arguments to be evaluated; quote “distributes” over its arguments, so '(+ 1 2) is like (list '+ '1 '2). You’ll end up with a symbol in your list, not a function.
An in-depth look at list and quote
In Scheme and Racket, quote and list are entirely different things, but since both of them can be used to produce lists, confusion is common and understandable. There is an incredibly important difference between them: list is a plain old function, while quote (even without the special ' syntax) is a special form. That is, list can be implemented in plain Scheme, but quote cannot be.
The list function
The list function is actually by far the simpler of the two, so let’s start there. It is a function that takes any number of arguments, and it collects the arguments into a list.
> (list 1 2 3)
(1 2 3)
This above example can be confusing because the result is printed as a quoteable s-expression, and it’s true, in this case, the two syntaxes are equivalent. But if we get slightly more complicated, you’ll see that it is different:
> (list 1 (+ 1 1) (+ 1 1 1))
(1 2 3)
> '(1 (+ 1 1) (+ 1 1 1))
(1 (+ 1 1) (+ 1 1 1))
What’s going on in the quote example? Well, we’ll discuss that in a moment, but first, take a look at list. It’s just an ordinary function, so it follows standard Scheme evaluation semantics: it evaluates each of its arguments before they get passed to the function. This means that expressions like (+ 1 1) will be reduced to 2 before they get collected into the list.
This behavior is also visible when supplying variables to the list function:
> (define x 42)
> (list x)
(42)
> '(x)
(x)
With list, the x gets evaluated before getting passed to list. With quote, things are more complicated.
Finally, because list is just a function, it can be used just like any other function, including in higher-order ways. For example, it can be passed to the map function, and it will work appropriately:
> (map list '(1 2 3) '(4 5 6))
((1 4) (2 5) (3 6))
The quote form
Quotation, unlike list, is a special part of Lisps. The quote form is special in part because it gets a special reader abbreviation, ', but it’s also special even without that. Unlike list, quote is not a function, and therefore it does not need to behave like one—it has rules of its own.
A brief discussion of Lisp source code
In Lisp, of which Scheme and Racket are derivatives, all code is actually made up of ordinary data structures. For example, consider the following expression:
(+ 1 2)
That expression is actually a list, and it has three elements:
the + symbol
the number 1
the number 2
All of these values are normal values that can be created by the programmer. It’s really easy to create the 1 value because it evaluates to itself: you just type 1. But symbols and lists are harder: by default, a symbol in the source code does a variable lookup! That is, symbols are not self-evaluating:
> 1
1
> a
a: undefined
cannot reference undefined identifier
As it turns out, though, symbols are basically just strings, and in fact we can convert between them:
> (string->symbol "a")
a
Lists do even more than symbols, because by default, a list in the source code calls a function! Doing (+ 1 2) looks at the first element in the list, the + symbol, looks up the function associated with it, and invokes it with the rest of the elements in the list.
Sometimes, though, you might want to disable this “special” behavior. You might want to just get the list or get the symbol without it being evaluated. To do this, you can use quote.
The meaning of quotation
With all this in mind, it’s pretty obvious what quote does: it just “turns off” the special evaluation behavior for the expression that it wraps. For example, consider quoteing a symbol:
> (quote a)
a
Similarly, consider quoteing a list:
> (quote (a b c))
(a b c)
No matter what you give quote, it will always, always spit it back out at you. No more, no less. That means if you give it a list, none of the subexpressions will be evaluated—do not expect them to be! If you need evaluation of any kind, use list.
Now, one might ask: what happens if you quote something other than a symbol or a list? Well, the answer is... nothing! You just get it back.
> (quote 1)
1
> (quote "abcd")
"abcd"
This makes sense, since quote still just spits out exactly what you give it. This is why “literals” like numbers and strings are sometimes called “self-quoting” in Lisp parlance.
One more thing: what happens if you quote an expression containing quote? That is, what if you “double quote”?
> (quote (quote 3))
'3
What happened there? Well, remember that ' is actually just a direct abbreviation for quote, so nothing special happened at all! In fact, if your Scheme has a way to disable the abbreviations when printing, it will look like this:
> (quote (quote 3))
(quote 3)
Don’t be fooled by quote being special: just like (quote (+ 1)), the result here is just a plain old list. In fact, we can get the first element out of the list: can you guess what it will be?
> (car (quote (quote 3)))
quote
If you guessed 3, you are wrong. Remember, quote disables all evaluation, and an expression containing a quote symbol is still just a plain list. Play with this in the REPL until you are comfortable with it.
> (quote (quote (quote 3)))
''3
(quote (1 2 (quote 3)))
(1 2 '3)
Quotation is incredibly simple, but it can come off as very complex because of how it tends to defy our understanding of the traditional evaluation model. In fact, it is confusing because of how simple it is: there are no special cases, there are no rules. It just returns exactly what you give it, precisely as stated (hence the name “quotation”).
Appendix A: Quasiquotation
So if quotation completely disables evaluation, what is it good for? Well, aside from making lists of strings, symbols, or numbers that are all known ahead of time, not much. Fortunately, the concept of quasiquotation provides a way to break out of the quotation and go back into ordinary evaluation.
The basics are super simple: instead of using quote, use quasiquote. Normally, this works exactly like quote in every way:
> (quasiquote 3)
3
> (quasiquote x)
x
> (quasiquote ((a b) (c d)))
((a b) (c d))
What makes quasiquote special is that is recognizes a special symbol, unquote. Wherever unquote appears in the list, then it is replaced by the arbitrary expression it contains:
> (quasiquote (1 2 (+ 1 2)))
(1 2 (+ 1 2))
> (quasiquote (1 2 (unquote (+ 1 2))))
(1 2 3)
This lets you use quasiquote to construct templates of sorts that have “holes” to be filled in with unquote. This means it’s possible to actually include the values of variables inside of quoted lists:
> (define x 42)
> (quasiquote (x is: (unquote x)))
(x is: 42)
Of course, using quasiquote and unquote is rather verbose, so they have abbreviations of their own, just like '. Specifically, quasiquote is ` (backtick) and unquote is , (comma). With those abbreviations, the above example is much more palatable.
> `(x is: ,x)
(x is: 42)
One final point: quasiquote actually can be implemented in Racket using a rather hairy macro, and it is. It expands to usages of list, cons, and of course, quote.
Appendix B: Implementing list and quote in Scheme
Implementing list is super simple because of how “rest argument” syntax works. This is all you need:
(define (list . args)
args)
That’s it!
In contrast, quote is a lot harder—in fact, it’s impossible! It would seem totally feasible, since the idea of disabling evaluation sounds a lot like macros. Yet a naïve attempt reveals the trouble:
(define fake-quote
(syntax-rules ()
((_ arg) arg)))
We just take arg and spit it back out... but this doesn’t work. Why not? Well, the result of our macro will be evaluated, so all is for naught. We might be able to expand to something sort of like quote by expanding to (list ...) and recursively quoting the elements, like this:
(define impostor-quote
(syntax-rules ()
((_ (a . b)) (cons (impostor-quote a) (impostor-quote b)))
((_ (e ...)) (list (impostor-quote e) ...))
((_ x) x)))
Unfortunately, though, without procedural macros, we can’t handle symbols without quote. We could get closer using syntax-case, but even then, we would only be emulating quote’s behavior, not replicating it.
Appendix C: Racket printing conventions
When trying the examples in this answer in Racket, you may find that they do not print as one would expect. Often, they may print with a leading ', such as in this example:
> (list 1 2 3)
'(1 2 3)
This is because Racket, by default, prints results as expressions when possible. That is, you should be able to type the result into the REPL and get the same value back. I personally find this behavior nice, but it can be confusing when trying to understand quotation, so if you want to turn it off, call (print-as-expression #f), or change the printing style to “write” in the DrRacket language menu.
The behavior you are seeing is a consequence of Scheme not treating symbols as functions.
The expression '(+ - * /) produces a value which is a list of symbols. That's simply because (+ - * /) is a list of symbols, and we are just quoting it to suppress evaluation in order to get that object literally as a value.
The expression (list + - * /) produces a list of functions. This is because it is a function call. The symbolic expressions list, +, -, * and / are evaluated. They are all variables which denote functions, and so are reduced to those functions. The list function is then called, and returns a list of those remaining four functions.
In ANSI Common Lisp, calling symbols as functions works:
[1]> (mapcar (lambda (f) (funcall f 1)) '(+ - * /))
(1 -1 1 1)
When a symbol is used where a function is expected, the top-level function binding of the symbol is substituted, if it has one, and everything is cool. In effect, symbols are function-callable objects in Common Lisp.
If you want to use list to produce a list of symbols, just like '(+ - * /), you have to quote them individually to suppress their evaluation:
(list '+ '- '* '/)
Back in the Scheme world, you will see that if you map over this, it will fail in the same way as the original quoted list. The reason is the same: trying to use a symbol objects as a functions.
The error message you are being shown is misleading:
expected a procedure that can be applied to arguments
given: '+
This '+ being shown here is (quote +). But that's not what the application was given; it was given just +, the issue being that the symbol object + isn't usable as a function in that dialect.
What's going on here is that the diagnostic message is printing the + symbol in "print as expression" mode, a feature of Racket, which is what I guess you're using.
In "print as expression" mode, objects are printed using a syntax which must be read and evaluated to produce a similar object. See the StackOverflow question "Why does the Racket interpreter write lists with an apostroph before?"

Syntax Error in Racket equal?

I am trying pass in a list input to conversation and have the function check to see if the first element in another list (keyword) matches to the first element of the list that user passed in. If the two match then output a zero otherwise pass the tail of the inputted list recursively back to itself.
(define keyword '(am I))
(define (conversation input)
(cond
((equal? (car keyword) (car input)) 0)
(else (conversation (cdr input)))))
The error I get is:
car: contract violation
expected: pair?
given: '()
I understand that equal? compares two elements, a pair, but what I do not understand is why it would create an error when the car of both lists are both exactly an element. Any help would be much appreciated, I'm assuming the solution is rather simple but I can't seem to see it.
My goal is create several functions that pattern match and output appropriate dialog but without the use of regular expressions or other libraries. There is no mandate not to use the two mentioned above but I would like to do it without them to get a better understanding of the logic and the code. Thanks for the help!
The first thing to consider is that you have no condition of failure. You assume there's either a match now with the car, or there will be a match later with the cdr. But there may be no match at all, and you will cdr down your list until your list is '(). As there is no such thing as the car of '() you are getting an error when you try to extract it. Therefore the first thing to do is make sure you've handled this case. I don't know what you intend to do in this case, so I have made the procedure return #f.
Next you consider what to do if the symbols do match. In your case you are choosing to return 0. This part seems to have no problems.
Finally, we consider what to do if the cars do not match. In this case we continue searching the input. This part seems to have no problems.
(define (conversation input)
(cond ((null? input) #f)
((eq? (car input) (car keyword))
0)
(else
(conversation (cdr input)))))

What is the difference between quote and list?

I know that you can use ' (aka quote) to create a list, and I use this all the time, like this:
> (car '(1 2 3))
1
But it doesn’t always work like I’d expect. For example, I tried to create a list of functions, like this, but it didn’t work:
> (define math-fns '(+ - * /))
> (map (lambda (fn) (fn 1)) math-fns)
application: not a procedure;
expected a procedure that can be applied to arguments
given: '+
When I use list, it works:
> (define math-fns (list + - * /))
> (map (lambda (fn) (fn 1)) math-fns)
'(1 -1 1 1)
Why? I thought ' was just a convenient shorthand, so why is the behavior different?
TL;DR: They are different; use list when in doubt.
A rule of thumb: use list whenever you want the arguments to be evaluated; quote “distributes” over its arguments, so '(+ 1 2) is like (list '+ '1 '2). You’ll end up with a symbol in your list, not a function.
An in-depth look at list and quote
In Scheme and Racket, quote and list are entirely different things, but since both of them can be used to produce lists, confusion is common and understandable. There is an incredibly important difference between them: list is a plain old function, while quote (even without the special ' syntax) is a special form. That is, list can be implemented in plain Scheme, but quote cannot be.
The list function
The list function is actually by far the simpler of the two, so let’s start there. It is a function that takes any number of arguments, and it collects the arguments into a list.
> (list 1 2 3)
(1 2 3)
This above example can be confusing because the result is printed as a quoteable s-expression, and it’s true, in this case, the two syntaxes are equivalent. But if we get slightly more complicated, you’ll see that it is different:
> (list 1 (+ 1 1) (+ 1 1 1))
(1 2 3)
> '(1 (+ 1 1) (+ 1 1 1))
(1 (+ 1 1) (+ 1 1 1))
What’s going on in the quote example? Well, we’ll discuss that in a moment, but first, take a look at list. It’s just an ordinary function, so it follows standard Scheme evaluation semantics: it evaluates each of its arguments before they get passed to the function. This means that expressions like (+ 1 1) will be reduced to 2 before they get collected into the list.
This behavior is also visible when supplying variables to the list function:
> (define x 42)
> (list x)
(42)
> '(x)
(x)
With list, the x gets evaluated before getting passed to list. With quote, things are more complicated.
Finally, because list is just a function, it can be used just like any other function, including in higher-order ways. For example, it can be passed to the map function, and it will work appropriately:
> (map list '(1 2 3) '(4 5 6))
((1 4) (2 5) (3 6))
The quote form
Quotation, unlike list, is a special part of Lisps. The quote form is special in part because it gets a special reader abbreviation, ', but it’s also special even without that. Unlike list, quote is not a function, and therefore it does not need to behave like one—it has rules of its own.
A brief discussion of Lisp source code
In Lisp, of which Scheme and Racket are derivatives, all code is actually made up of ordinary data structures. For example, consider the following expression:
(+ 1 2)
That expression is actually a list, and it has three elements:
the + symbol
the number 1
the number 2
All of these values are normal values that can be created by the programmer. It’s really easy to create the 1 value because it evaluates to itself: you just type 1. But symbols and lists are harder: by default, a symbol in the source code does a variable lookup! That is, symbols are not self-evaluating:
> 1
1
> a
a: undefined
cannot reference undefined identifier
As it turns out, though, symbols are basically just strings, and in fact we can convert between them:
> (string->symbol "a")
a
Lists do even more than symbols, because by default, a list in the source code calls a function! Doing (+ 1 2) looks at the first element in the list, the + symbol, looks up the function associated with it, and invokes it with the rest of the elements in the list.
Sometimes, though, you might want to disable this “special” behavior. You might want to just get the list or get the symbol without it being evaluated. To do this, you can use quote.
The meaning of quotation
With all this in mind, it’s pretty obvious what quote does: it just “turns off” the special evaluation behavior for the expression that it wraps. For example, consider quoteing a symbol:
> (quote a)
a
Similarly, consider quoteing a list:
> (quote (a b c))
(a b c)
No matter what you give quote, it will always, always spit it back out at you. No more, no less. That means if you give it a list, none of the subexpressions will be evaluated—do not expect them to be! If you need evaluation of any kind, use list.
Now, one might ask: what happens if you quote something other than a symbol or a list? Well, the answer is... nothing! You just get it back.
> (quote 1)
1
> (quote "abcd")
"abcd"
This makes sense, since quote still just spits out exactly what you give it. This is why “literals” like numbers and strings are sometimes called “self-quoting” in Lisp parlance.
One more thing: what happens if you quote an expression containing quote? That is, what if you “double quote”?
> (quote (quote 3))
'3
What happened there? Well, remember that ' is actually just a direct abbreviation for quote, so nothing special happened at all! In fact, if your Scheme has a way to disable the abbreviations when printing, it will look like this:
> (quote (quote 3))
(quote 3)
Don’t be fooled by quote being special: just like (quote (+ 1)), the result here is just a plain old list. In fact, we can get the first element out of the list: can you guess what it will be?
> (car (quote (quote 3)))
quote
If you guessed 3, you are wrong. Remember, quote disables all evaluation, and an expression containing a quote symbol is still just a plain list. Play with this in the REPL until you are comfortable with it.
> (quote (quote (quote 3)))
''3
(quote (1 2 (quote 3)))
(1 2 '3)
Quotation is incredibly simple, but it can come off as very complex because of how it tends to defy our understanding of the traditional evaluation model. In fact, it is confusing because of how simple it is: there are no special cases, there are no rules. It just returns exactly what you give it, precisely as stated (hence the name “quotation”).
Appendix A: Quasiquotation
So if quotation completely disables evaluation, what is it good for? Well, aside from making lists of strings, symbols, or numbers that are all known ahead of time, not much. Fortunately, the concept of quasiquotation provides a way to break out of the quotation and go back into ordinary evaluation.
The basics are super simple: instead of using quote, use quasiquote. Normally, this works exactly like quote in every way:
> (quasiquote 3)
3
> (quasiquote x)
x
> (quasiquote ((a b) (c d)))
((a b) (c d))
What makes quasiquote special is that is recognizes a special symbol, unquote. Wherever unquote appears in the list, then it is replaced by the arbitrary expression it contains:
> (quasiquote (1 2 (+ 1 2)))
(1 2 (+ 1 2))
> (quasiquote (1 2 (unquote (+ 1 2))))
(1 2 3)
This lets you use quasiquote to construct templates of sorts that have “holes” to be filled in with unquote. This means it’s possible to actually include the values of variables inside of quoted lists:
> (define x 42)
> (quasiquote (x is: (unquote x)))
(x is: 42)
Of course, using quasiquote and unquote is rather verbose, so they have abbreviations of their own, just like '. Specifically, quasiquote is ` (backtick) and unquote is , (comma). With those abbreviations, the above example is much more palatable.
> `(x is: ,x)
(x is: 42)
One final point: quasiquote actually can be implemented in Racket using a rather hairy macro, and it is. It expands to usages of list, cons, and of course, quote.
Appendix B: Implementing list and quote in Scheme
Implementing list is super simple because of how “rest argument” syntax works. This is all you need:
(define (list . args)
args)
That’s it!
In contrast, quote is a lot harder—in fact, it’s impossible! It would seem totally feasible, since the idea of disabling evaluation sounds a lot like macros. Yet a naïve attempt reveals the trouble:
(define fake-quote
(syntax-rules ()
((_ arg) arg)))
We just take arg and spit it back out... but this doesn’t work. Why not? Well, the result of our macro will be evaluated, so all is for naught. We might be able to expand to something sort of like quote by expanding to (list ...) and recursively quoting the elements, like this:
(define impostor-quote
(syntax-rules ()
((_ (a . b)) (cons (impostor-quote a) (impostor-quote b)))
((_ (e ...)) (list (impostor-quote e) ...))
((_ x) x)))
Unfortunately, though, without procedural macros, we can’t handle symbols without quote. We could get closer using syntax-case, but even then, we would only be emulating quote’s behavior, not replicating it.
Appendix C: Racket printing conventions
When trying the examples in this answer in Racket, you may find that they do not print as one would expect. Often, they may print with a leading ', such as in this example:
> (list 1 2 3)
'(1 2 3)
This is because Racket, by default, prints results as expressions when possible. That is, you should be able to type the result into the REPL and get the same value back. I personally find this behavior nice, but it can be confusing when trying to understand quotation, so if you want to turn it off, call (print-as-expression #f), or change the printing style to “write” in the DrRacket language menu.
The behavior you are seeing is a consequence of Scheme not treating symbols as functions.
The expression '(+ - * /) produces a value which is a list of symbols. That's simply because (+ - * /) is a list of symbols, and we are just quoting it to suppress evaluation in order to get that object literally as a value.
The expression (list + - * /) produces a list of functions. This is because it is a function call. The symbolic expressions list, +, -, * and / are evaluated. They are all variables which denote functions, and so are reduced to those functions. The list function is then called, and returns a list of those remaining four functions.
In ANSI Common Lisp, calling symbols as functions works:
[1]> (mapcar (lambda (f) (funcall f 1)) '(+ - * /))
(1 -1 1 1)
When a symbol is used where a function is expected, the top-level function binding of the symbol is substituted, if it has one, and everything is cool. In effect, symbols are function-callable objects in Common Lisp.
If you want to use list to produce a list of symbols, just like '(+ - * /), you have to quote them individually to suppress their evaluation:
(list '+ '- '* '/)
Back in the Scheme world, you will see that if you map over this, it will fail in the same way as the original quoted list. The reason is the same: trying to use a symbol objects as a functions.
The error message you are being shown is misleading:
expected a procedure that can be applied to arguments
given: '+
This '+ being shown here is (quote +). But that's not what the application was given; it was given just +, the issue being that the symbol object + isn't usable as a function in that dialect.
What's going on here is that the diagnostic message is printing the + symbol in "print as expression" mode, a feature of Racket, which is what I guess you're using.
In "print as expression" mode, objects are printed using a syntax which must be read and evaluated to produce a similar object. See the StackOverflow question "Why does the Racket interpreter write lists with an apostroph before?"

Scheme pass-by-reference

How can I pass a variable by reference in scheme?
An example of the functionality I want:
(define foo
(lambda (&x)
(set! x 5)))
(define y 2)
(foo y)
(display y) ;outputs: 5
Also, is there a way to return by reference?
See http://community.schemewiki.org/?scheme-faq-language question "Is there a way to emulate call-by-reference?".
In general I think that fights against scheme's functional nature so probably there is a better way to structure the program to make it more scheme-like.
Like Jari said, usually you want to avoid passing by reference in Scheme as it suggests that you're abusing side effects.
If you want to, though, you can enclose anything you want to pass by reference in a cons box.
(cons 5 (void))
will produce a box containing 5. If you pass this box to a procedure that changes the 5 to a 6, your original box will also contain a 6. Of course, you have to remember to cons and car when appropriate.
Chez Scheme (and possibly other implementations) has a procedure called box (and its companions box? and unbox) specifically for this boxing/unboxing nonsense: http://www.scheme.com/csug8/objects.html#./objects:s43
You can use a macro:
scheme#(guile-user)> (define-macro (foo var)`(set! ,var 5))
scheme#(guile-user)> (define y 2)
scheme#(guile-user)> (foo y)
scheme#(guile-user)> (display y)(newline)
5
lambda!
(define (foo getx setx)
(setx (+ (getx) 5)))
(define y 2)
(display y)(newline)
(foo
(lambda () y)
(lambda (val) (set! y val)))
(display y)(newline)
Jari is right it is somewhat unscheme-like to pass by reference, at least with variables. However the behavior you want is used, and often encouraged, all the time in a more scheme like way by using closures. Pages 181 and 182(google books) in the seasoned scheme do a better job then I can of explaining it.
Here is a reference that gives a macro that allows you to use a c like syntax to 'pass by reference.' Olegs site is a gold mine for interesting reads so make sure to book mark it if you have not already.
http://okmij.org/ftp/Scheme/pointer-as-closure.txt
You can affect an outer context from within a function defined in that outer context, which gives you the affect of pass by reference variables, i.e. functions with side effects.
(define (outer-function)
(define referenced-var 0)
(define (fun-affects-outer-context) (set! referenced-var 12) (void))
;...
(fun-affects-outer-context)
(display referenced-var)
)
(outer-function) ; displays 12
This solution limits the scope of the side effects.
Otherwise there is (define x (box 5)), (unbox x), etc. as mentioned in a subcomment by Eli, which is the same as the cons solution suggested by erjiang.
You probably have use too much of C, PHP or whatever.
In scheme you don't want to do stuff like pass-by-*.
Understand first what scope mean and how the different implementation behave (in particular try to figure out what is the difference between LISP and Scheme).
By essence a purely functional programming language do not have side effect. Consequently it mean that pass-by-ref is not a functional concept.

Resources