LISP stands for LISt Processing. Using Lisp requires you to make use of so many brackets that soon enough you abandon the language altogether, and switch to Python. A basic mathematical expression, say *3 + 4* is written in this way

```
(+ 3 4)
```

To assign a value, say *4*, to a variable, say *x*, do the following

```
(define x 4)
```

Say we want to define a function that takes in a number and returns the square of that number. This is how we do it

```
(define (square x) (* x x))
```

The interpreter in our case uses *applicative order evaluation*, which means that it evaluates the arguments in the innermost bracket first, and then moves outwards. A different method of evaluation is *normal order evaluation*, where the interpreter evaluates arguments from left-to-right instead. The applicative order method is more efficient as compared to the normal order method, as the normal order method may end up evaluating the same thing multiple times.

#### Conditional Expressions

Now we come to conditional expressions. Conditional expressions follow this format

```
(cond (<p1> <e1>)
(<p2> <e2>)
(<p3> <e3>)
...
(<pn> <en>))
```

The *p*s are all *predicates*, ie, the expression whose value when interpreted returns a true or false. The *e*s are the *consequent expressions*, ie, the values on which the predicate operates. An example mentioned in the SICP book is an absolute value function, ie, a function that returns the absolute value of the number passed in. It looks like this

```
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
```

Apart from the crazy number of brackets present in this declaration, everything else works. Look at the last expression: `(- x)`

. It tells the interpreter to return the negative of the value passed in.

Another conditional expression is an if expression, which has the form

```
(if <predicate> <consequent> <alternative>)
```

Read the general form in this way: if (this) (then this) (else this). With the *if* expression, our absolute value statement can be simplified, like so

```
(define (abs x)
(if (< x 0)(- x) x))
```

We’ve lost most of the brackets, and almost all of the headache of the previous expression. Btw, in my DrRacket environment, when I chose R5RS as the language, it told me that it already had a library function called abs, so I had to name my function something else.

There are three other logical composition operations that are commonly used:

`(and <e1> ... <en>)`

If any of (e1 to en) evaluate to false, the expression returns false.`(or <e1> ... <en>)`

If any of (e1 to en) evaluates to true, the expression returns true.`(not <e>)`

If e is false, then the expression is true.

### Exercise 1.2

```
(/
(+ 5 4 (- 2 (- 3 (+ 6 4/5))))
(* 3 (- 6 2) (- 2 7)))
```

### Exercise 1.3

```
; STEP 1
; Define a function that squares a number
(define (sq x) (* x x))
; STEP 2
; Now use that to add up the numbers
; after finding the two largest numbers
(def (sum-sq-max x y z)
(if (and (> x z) (> y z)) (+ (sq x) (sq y))
(if (and (> y x) (> z x)) (+ (sq z) (sq y)) (+ (sq x) (sq z)))
)
)
```

### Exercise 1.4

```
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b)
)
```

This function adds the face value of `a`

(i.e, considering the sign with the number) to the absolute value of `b`

(i.e, discarding the sign).

### Exercise 1.5

```
; Function one defined by Ben Bitdiddle
(define (p) (p))
; Function two defined by Ben Bitdiddle
(define (test x y)
(if (= x 0) 0 y)
)
```