# Hints for Assignment 1

## Common errors

#### Procedure calls

Remember the rules for evaluating a procedure call:
- A procedure call is formatted as a list:
- The first element must be a procedure
- The rest of the elements are evaluated to detemine the argument values

You can't use C/C++ style for calling functions. The following is illegal:
ackermann(i j)

It should be:
(ackermann i j)

#### Nesting `if` statements

Several students have written functions like the following example:
(define (example x)
(if (> x 3)
89)
(if (< x -78)
(* x 4)
(/ x 7))

Although this is legal Scheme, it will not do what you think it will
do. This is an example of thinking procedurally, rather than
fuctionally.
A procedure will return only one value, so you really only get one
expression to evaluate. How do you do two things with just one
expression? In this case, you have to think in terms of nested
`if` statements. The way the above code should be written is:

(define (example x)
(if (> x 3)
89
(if (< x -78)
(* x 4)
(/ x 7))))

## Hints

#### Problem 7

Use a "helper" function. For example:
(define (list-sums l)
(list-sums-helper <arg1> <arg2>))
(define (list-sums-helper <arg1-name> <arg2-name>)
<procedure body>)

The basic idea is that the "main" function sets up a call to a
"helper" function that actually does the work.
Here is a concrete example. Consider the factorial function we did on
the first day:

(define (fac n)
(if (= n 0)
1
(* n (fac (- n 1)))))

Here is a version that uses a helper function:
(define (fac n)
(fac-helper n 1))
(define (fac-helper n result)
(if (= n 0)
result
(fac-helper (- n 1) (* result n))))

Essentially what happens is that the result is built up in one of the
arguments to the recursive call, and at the end, the result can be
returned directly without unwinding the chain of recursive calls.
whuang@cs.rpi.edu

Last updates: September 6, 1999