Home > Lisp, Scheme > Two tricky areas in Lisp

Two tricky areas in Lisp

While learning Scheme, I’ve discovered two tricky places which seemed easy at beginning, but turned to be more complex. Now I seem to understand those things, and want to share my experience. All examples are provided in Scheme, but I’m almost sure that same things are also true for Common Lisp.

First area is variables.

When you type

(define a 10)

then you create a variable and initialize it with value 10. There are no way to create a variable without initial value. Now, suppose that you’ll type following:

(define b a)



Yes, that’s natural to expect that evaluating b will result in 10. Now, suppose you’ll do following:

(set! a 20)




Result of evaluation depends on what semantics does (define b a) have. I thought that it means that b and a point to same place in memory. In this case, changing value of a should also affect b, and evaluation of last expression should return 20. However, if you’ll try this example yourself, you’ll see that result will be 10. That means that (define b a) has different semantics.

Scheme is simple, and (define b a) should have exactly the same meaning as (define a 10) : create new variable and initialize it with some value. In both cases last element is a value, the only difference is that in first case evaluation of a results in value 10, in second case value 10 is provided immediately.

There are no way in Scheme to make several variables to point on same integer value. But two variables can point on same cons cell. So, that’s a language element which works as “pointer” or “reference”.

Second area is “lexical scope”.

Initially I thought that it means that following code:

(define x 5)

(define (f) x)



(set! x 10)


Will result in 5. My understanding was that “at place where f was defined value of x was 5, so that value is captured by definition and is not affected by later changes of x”. I thought that was a meaning of “lexical scope”.

As you expect, I was wrong. Above example will print 10. It turns out that meaning of “lexical scope” is totally different. I thought that “scope” means “value scope”, but it should be “name scope”. Look at example:

(define x 5)

(define (f) x)



(let ((x 20)) (list (f) x))

(5 20)

When f is evaluated second time, name global variable x is shadowed by local variable x, so name x is evaluated to 20. But at time of definition of f name x was refered to global variable, and each time f is evaluated it uses global variable.

That’s all for today. I’m very pleased with Scheme, it’s semantics are simple and consistent. Most of my confusion came from my previous experience with other programming languages, so I’m often expect somehting complex and context-dependent.

Update: After I’ve posted that there was a discussion in comp.lang.scheme about reference/value semantics.

Another update: Discussion of these subjects continues in my future post

Categories: Lisp, Scheme
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: