Lab 10 - Scheme
A lab aimed at getting accustomed with thinking and writing Scheme, the first exercises are mostly easy, all you have to do is translate what you already know how to do in Python into Scheme.
I enjoyed writing Scheme so much, I also did the Extras. I found the later ones (Q8, Q9) to be a nice challenge. 👍
Instructions: https://inst.eecs.berkeley.edu/~cs61a/su19/lab/lab10/
Solution: https://github.com/tomthestrom/cs61a/blob/master/lab/lab10/lab10.scm
Solution - extras: https://github.com/tomthestrom/cs61a/blob/master/lab/lab10/lab10_extra.scm
Q1 is non-coding - What Would Scheme Do?
Q2: Over or Under
Define a procedure over-or-under
which takes in a number x
and a number y
and returns the following:
-1 if
x
is less thany
0 if
x
is equal toy
1 if
x
is greater thany
Q2: Solution
Q3: Filter Lst
Write a procedure filter-lst
, which takes a predicate f
and a list lst
, and returns a new list containing only elements of the list that satisfy the predicate. The output should contain the elements in the same order that they appeared in the original list.
Q3: Solution
Q4: Make Adder
Write the procedure make-adder
which takes in an initial number, num
, and then returns a procedure. This returned procedure takes in a number x
and returns the result of x + num
.
Q4: Solution
Extras:
Q5: Make a List
Create the list with the following box-and-pointer diagram:
Q5: Solution
Q6: Compose
Write the procedure composed
, which takes in procedures f
and g
and outputs a new procedure. This new procedure takes in a number x
and outputs the result of calling f
on g
of x
.
Q6: Solution
Q7: Remove
Implement a procedure remove
that takes in a list and returns a new list with all instances of item
removed from lst
. You may assume the list will only consist of numbers and will not have nested lists.
Hint: You might find the filter-lst
procedure useful.
Q7: Solution
Q8: No Repeats
Implement no-repeats
, which takes a list of numbers s
as input and returns a list that has all of the unique elements of s
in the order that they first appear, but no repeats. For example, (no-repeats (list 5 4 5 4 2 2))
evaluates to (5 4 2)
.
Q8: Solution
Q9: Substitute
Write a procedure substitute
that takes three arguments: a list s
, an old
word, and a new
word. It returns a list with the elements of s
, but with every occurrence of old
replaced by new
, even within sub-lists.
Q9: Solution
Last updated