| CS 330 | Home | Schedule | Resources |
Estimated time: 3 hours
Acceptance tests for HW 7
The objective of this homework is to practice recursive programming techniques on lists. Do not forget to use invariants to guarantee the correctness of your functions.
<List T> ::= nil | T '|' <List T> [a b c] is in fact a shorthand to denote
the list made up of the three elements A B
and C. This list can also be written A|(B|(C|nil)).
This notation emphasizes the recursive definition of a list.[] [a][a [b c] d][(proc{$} {Browse 1} end) (proc{$} {Browse 2}
end)][is a list][ [a p] ]this as the first
element of List #5.[[b c] d].Head and Tail
which return, respectively, the head and the tail of a list passed in
parameter. (That is, Head returns the head of the list
and Tail returns the tail.)Nth which returns
the Nth element of a list passed in as a parameter. Nth returns
0 if the list contains fewer than N elements. Think of the
recursive definition of the list to identify the various cases!Length, which
returns the length of a list passed in as a parameter. Think of the recursive
definition of the list to identify the various cases! {Browse {Length [r a p h]}} % displays 4
{Browse {Length [[r a p] h]}} % displays 2
{Browse {Length [[r a p h]]}} % displays 1
Some general functions on lists. Here is a list of commonly used functions on lists. Choose TWO to implement, and program by reasoning about them in an inductive way on the structure of the
lists. Do NOT use the built-in version. Try to guess the answer to the last example for each function, then lookup up the built-in version (or the closest you can find) for all five, and use it to check your guess. Submit the code you run and its output.
{ Member X Ys } returns true
if X is an element of the list Ys,
false if not.
{Browse {Member p [r a p h]}} % returns true
{Browse {Member c [r a p h]}} % returns false
{Browse {Member p [r [a p] h]}} % what is the result?
{ Append Xs Ys } returns the concatenation of the
lists Xs and Ys.
{Browse {Append [r a] [p h]}} % returns [r a p h]
{Browse {Append [r [a]] [p h]}} % returns [r [a] p h]
{Browse {Append [r a] [[p h]]}} % what is the result?
{ Take Xs N } returns a list containing the first
N elements of the list Xs.
{Browse {Take [r a p h] 2}} % returns [r a]
{Browse {Take [r a p h] 7}} % returns [r a p h]
{Browse {Take [r [a p] h] 2}} % what is the result?
{ Drop Xs N } returns the list Xs
without its N first elements.
{Browse {Drop [r a p h] 2}} % returns [p h]
{Browse {Drop [r a p h] 7}} % returns nil
{Browse {Drop [r [a p] h] 2}} % what is the result?
{ Interval Xs N1 N2 } returns a list which contains
all the elements of Xs from indices N1 to
N2.
{Browse {Interval [r a p h] 2 3}} % returns [a p]
{Browse {Interval [r a p h] 3 2}} % returns nil
{Browse {Interval [r [a p] h] 2 2}} % what is the result?
Let us multiply. Write a function MultList
such that { MultList L } returns the result of the multiplication
of the elements of the list L. For example,
{ Browse { MultList [ 1 2 3 4 ] } } % displays 24
Write a function now MultList2 such that the recursive
call is the last statement executed (tail-recursive). Use an accumulator.
Formalize the invariant which is associated with it.
case X
of nil then instruction1
[] Head|Tail then instruction2
end X is the value which one tries to match.
If X is equal to the empty list (nil)
instruction 1 will be carried out. If not,
if X has the shape of a nonempty list (with a head and
a tail (cfr. definition of the list), instruction 2
will be carried out.ListType which returns emptylist if the variable
passed in as a parameter is an empty list, nonemptylist if
the variable is a nonempty list and other if the variable is
of another type (integer, atom...).MultList by using pattern
matching. Call it PMultList.Fact such that { Fact N }
returns a list of the first N factorial numbers in ascending
order. For example,
{ Browse { Fact 4 } } % displays [ 1 2 6 24 ]