schemeracketplai

how to resolve this error "reference to an identifier before its definition: with"


I am studying "Programming Languages: Application and Interpretation" and I can run the first two chapters' examples in DrRacket 5.2.1 after executing #lang plai. but when I typed the 3rd chapter's first example as below:

(with (x 5) (+ x x))

I got the following error:

reference to an identifier before its definition: with

I did not find the definition of with in this book. Do I need a library?


Solution

  • The with construct is not something that you use in your own programs -- it's something that exists in the language that you define instead. In other words, it's something that you implement not something that you use. Note that the books always uses it with curly braces, {with {x ...} ...}, and that's intended to avoid this exact confusion that you're having -- curly braces are always used in code that is in the language you implement, and round parentheses are used for your own implementation's code.

    Note that defining your own with is simple in Racket, but it would be wrong, and is likely to get you more confused. Instead of trying to use it in Racket, you should just follow the book, and at the end of chapter 3 you will have a working interpreter for the WAE language -- and then you'll be able to use it to run WAE programs that use with.

    As a side note, if you're looking for a Racket form that is similar to with, then look into let -- the only difference is that let allows you to specify multiple bindings instead of just one.