HW6 - Scheme, Tail Recursion, Macros
Instructions: https://inst.eecs.berkeley.edu/~cs61a/su19/hw/hw06/
Solution: https://github.com/tomthestrom/cs61a/blob/master/homeworks/hw06/hw06.scm
Scheme
Q1: Thane of Cadr
Define the procedures cadr
and caddr
, which return the second and third elements of a list, respectively:
Q1: Solution
Q2: Unique
Implement unique
, which takes in a list s
and returns a new list containing the same elements as s
with duplicates removed.
Q2: Solution
Q3: Cons All
Implement cons-all
, which takes in an element first
and a list of lists rests
, and adds first
to the beginning of each list in rests
:
Q3: Solution
Q4: List Change
Implement the list-change
procedure, which lists all of the ways to make change for a positive integer total
amount of money, using a list of currency denominations, which is sorted in descending order. The resulting list of ways of making change should also be returned in descending order.
To make change for 10 with the denominations (25, 10, 5, 1), we get the possibilities:
To make change for 5 with the denominations (4, 3, 2, 1), we get the possibilities:
Therefore, your function should behave as follows for these two inputs
Q4: Solution
Tail Recursion
Q5: Replicate
Below is an implementation of the replicate
function, which was seen in Discussion 08. replicate
takes in an element x
and an integer n
, and returns a list with x
repeated n
times.
Update this implementation of replicate
to be tail recursive. It should have identical functionality to the non-tail recursive version.
Q5: Solution
Q6: Accumulate
Fill in the definition for the procedure accumulate
, which combines the first n
natural numbers according to the following parameters:
combiner
: a function of two argumentsstart
: a number with which to start combiningn
: the number of natural numbers to combineterm
: a function of one argument that computes the nth term of a sequence
For example, we can find the product of all the numbers from 1 to 5 by using the multiplication operator as the combiner
, and starting our product at 1:
We can also find the sum of the squares of the same numbers by using the addition operator as the combiner
and square
as the term
:
Q6: Solution
Q7: Tail Recursive Accumulate
Update your implementation of accumulate
to be tail recursive. It should still pass all the tests for "regular" accumulate
!
You may assume that the input combiner
and term
procedures are properly tail recursive.
Q7: Solution
Macros
Q8: List Comprehensions
Recall that list comprehensions in Python allow us to create lists out of iterables:
Use a macro to implement list comprehensions in Scheme that can create lists out of lists. Specifically, we want a list-of
macro that can be called as follows:
Calling list-of
will return a new list constructed by doing the following for each element in <list>
:
Bind
<name>
to the element.If
<conditional-expression>
evaluates to a truth-y value, evaluate<map-expression>
and add it to the result list.
Q8: Solution
Last updated