The Define Command

Chapter: The Define Command

Have you ever wondered how s can be given the value Harry? In other words, can we do something to s so that it will evaluate to Harry? The answer is "Yes!" We can define s to be Harry. define is a keyword in scheme; it does not evaluate its first argument. Keywords generally wait to evaluate one or more of their arguments, whereas functions always evaluate every argument before doing anything else. Try the following commands:

> (define brainless 3)
> brainless
3
> (+ brainless 4) ; add 4 to the value of brainless
7
> (define my-brother 'Harry)
> my-brother
Harry
> (define a-list '((foo) bar (baz qux) quux))
> a-list
((foo) bar (baz qux) quux)
> (car a-list)
(foo)
> (define adding-machine +) 
> (adding-machine brainless 4) ; add the value of brainless to 4
7

Notice that Harry needs to be quoted inside the definition. This is necessary because the value you wanted to assign to my-brother is the atom Harry itself, not the value of Harry.

If you typed (define my-brother brainless), Scheme would first evaluate brainless, which returns 3 (from before), and then it would assign this value to the atom my-brother. Try it and then try evaluating my-brother to see what its new value is.

This would be a good time to ask your instructor about null? and eq?.


rhyspj@cs.gwu.edu