CS230
Scheme in-class exercises
Wednesday 11/07/2022
Note: problems 4 and 5 have you recreate code we wrote in class on 10/31/2022
that I posted. Try to write these functions without looking at the code posted
on 10/31/2022. Feel free to look at any code posted on the other days.
1. Write a function named allEven that takes in a list as a parameter and
assumes that the list is either empty or only contains integers, and determines
whether all those integers are even or not. Returns #t if all are even, #f
otherwise.
e.g.
(allEven '(6 4 8 1 10)) ; should result in #f, because at least 1 is odd
(allEven '(6 4 8 12 10)) ; should result in #t, because all are even
2. Write a function named sumUp that takes in a list as a parameter (assumed to
only contain numbers). sumUp should return the sum of those numbers
e.g.
(sumUp '(1 2 3 4 10)) ; should return 20
3. Write a function named sumUpPositives that takes in a list as a parameter
(assumed to only contain numbers). sumUpPositives should return the sum of
only the positive numbers.
e.g.
(sumUpPositives '(1 -8 2 -67.5 3 -2 4 10)) ; should return 20
4. Write a function named removeFirst that takes in two parameters,
the first is expected to be an atom and the second is expected to be a list.
removeFirst should return the list with the first occurence of atom (if found)
removed from it.
e.g.
(remove1st 'y '(x y z)) ; should result in '(x z)
(remove1st 'x '(x)) ; should result in '()
(remove1st 'y '(x y z y)) ; should result in '(x z y)
(remove1st 'y '(a b c)) ; should result in '(a b c)
HINT: you must return a list that is built up from
cars of lists so you should use cons
e.g.
if lis is '(a b c)
(cons (car lis) (cdr lis)) ; results in the original list '(a b c)
5. Write a function named removeAll that takes in two parameters,
the first is expected to be an atom and the second is expected to be a list.
removeFirst should return the list with the all occurences of atom (if found)
removed from it.
e.g.
(removeAll 'y '(x y z y)) ; should result in '(x z)
(removeAll 'y '(x y z)) ; should result in '(x z)
(removeAll 'x '(x a x b x c x)) ; should result in '(a b c)
(removeAll 'y '(a b c)) ; should result in '(a b c)
6. Write a function thirdElement to return the 3rd element in the list argument.
If the argument is not a list or is a list containing fewer than 3 elements,
then return 0.
7. Write a Scheme function named odd-list that takes one parameter which is a
number (real or integer) and returns a list containing all the non-negative
odd integers strictly less than that number in increasing order.
Here are some examples:
(odd-list 0) should return ()
(odd-list 1) should return ()
(odd-list 2) should return (1)
(odd-list 3) should return (1)
(odd-list 4) should return (1 3)
(odd-list 5) should return (1 3)
(odd-list 6) should return (1 3 5)
(odd-list 7.2) should return (1 3 5 7)
; note: try to get this to return a list of integers, not (1.0 3.0 5.0 7.0)
(odd-list -3) should return ()