Let’s say you have a list of points that are put together with `cons`

. You can `car`

to get the x-value and `cdr`

for the y-value.

Now let’s say you need to add the values up, for instance, all the x-values. You can do it really easily with a *neat* function that’s included in Scheme. It’s called `fold`

.

The documentation on fold allows you to to chain a procedure such that once run, it will do it again with the return value of the previous run as the next iteration’s input. That sounds confusing but this example demonstrates it quite well.

(define (sum-x-values lst) (fold (lambda (p b) (+ (car p) b)) 0 lst) )

What does this do? The `p`

is the value from the list you’re working with. So in this case it would be a `cons`

pair of x-y values. The `b`

value is the *return* value from the previous run. The `car`

will pull the x-value from the object, and if you wanted y-values, you can do `car`

on p. Then it’s summed up and returned until it’s done. The `0`

is the initial value the first sum is added to – so that’s pretty important.

This is a great way to compact down an annoying series of iterators and recursions.

And a thanks to Sam for pointing this method out.

Ooh, I knew that one!

Also, the site looks different now.

What do you think of the new design?