# SICP Solutions

### Chapter 4, Metalinguistic Abstraction

#### Special form implementation

The logic for both and and or is same except that what we do for ‘true’ case in and, we do for the ‘false’ case in or.

So, I implemented a common procedure.

One thing to note is the returned values by and and or expressions. Currently these values are the internal values of the language. Or we are using the same primitives as of the implementation language for boolean case too!

It may then occur that we should add boolean check in self-evaluating?, but this is not so because we have defined variables(in book), true and false. So instead of using #t, or #f we can get our code work using true and false.

How difficult it would be to implement our own numbers/booleans instead of using from the implementing language? I tried for boolean to use ‘yes and ‘no but it turned out it will take some more thinking so abandoned the effort.

Let’s first check the output(note all the possible inputs including and/or with 0 arguments & nesting):

(for short circuiting, we should not even evaluate the remaining part even if evaluating it causes error - eg (and false t) - should return false even if t is not defined - see last output example.)

Here are the corresponding changes:

#### Derived form implementation

Similar to special form implementation, here also, I have combined the logic in a single procedure:

(Add this in eval: ((cond? exp) (eval (cond->if exp) env)))

This also works exactly like the previous section(short-circuiting/nesting). I have tested it for same inputs.

Few things to notice:

• I used 'true and 'false here instead of implementing language true and false values as opposed to the special form version. Why? Because after conversion, the syntax should conform to our language. We are not evaluating here but just converting to special form!
• As an effect of above point, no eval calls in the conversion procedures.
• Note that first is used two times. Thus it will be evaluated twice! This has two problems: (i) Efficiency (ii) If an expression has any side effect(assignment), we have more trouble!

We can avoid this using memoization but I have skipped this probably I think this may get covered later in the book itself.