Exercise 1.
Plural
"Did you mean: recursion"
In this lab we will dive into functional programming and recursion. In short, recursion is idea of having a procedure solve some big problem by making it a little bit smaller somehow and then calling itself. When it calls itself, it makes the problem smaller yet again. This continues until the problem is small enough to be trivially solved. Recursion can be hard to get used to if you have never used it before. Some things to remember when programming recursively are:
If this seems weird to you, take a look at the previous lab, Recursion and Scheme
Plural
Max Sum of Squares
Duplicates Removed
Pig Latin
Write a procedure pigl
that takes a word as an
argument and returns that word in pig latin.
Here are the rules for pig latin:
Here are some examples:
(pigl 'hello) ; ellohay
(pigl 'open) ; openay
(pigl 'scheme) ; emeschay
Count-Word
People who know Scheme: Don't use the CS 3 higher-order procedures such as every in these problems; use recursion.
The Return of new-if
Squares
Switch
Ordered?
Ends-E
Most versions of Lisp provide and
and or
procedures like the ones we've seen.In principle there is no reason why these
can't be ordinary procedures, but some versions of Lisp make them special forms.
Suppose, for example, we evaluate (or (= x 0) (= y 0) (= z 0))
If or
is an ordinary procedure, all three argument expressions will
be evaluated before or
is invoked. But if the variable x
has the value 0, we know that the entire expression has to be true regardless of
the values of y and z. A Lisp interpreter in which
or
is a special form can evaluate the arguments one by one until
either a true one is found or it runs out of arguments.
Do the following reading: