Midterm Exam for CS105-Fall 1997
Warming Up
- What is the general rule for evaluating an expression
(f arg1 arg2 .. argn)?
- What is a special form? Give three examples and
explain why they are special forms.
- What distinguishes a higher-order function from
a "normal" function? Give two examples of higher-order functions.
Conditionals
Write a function season
that tells you what season you are in on a given date
(specified by month and day). Assume the following fixed dates
for the seasons:
Spring: 3/21 - 6/20
Summer: 6/21 - 9/20
Autumn: 9/21 - 12/20
Winter: 12/21 - 3/20.
For example (season 6 21) => Summer and
(season 10 27) => Autumn
Higher-order functions and lambda
- Write a function occur-a which takes as an input a word
and counts the number of times the letter a occurs in it.
For example (occur-a 'abracadabra) => 5 and
(occur-a "") => 0. { Hint: use higher-order
functions like keep}.
- Write a function occur which takes as input a
letter and a word and counts the number of times the letter occurs in the word.
For example (occur 'a 'abracadabra) => 5,
(occur 'b 'abracadabra) => 2, and
(occur 'z 'Merlyn) => 0. { Hint: you will have to
use higher-order functions and lambda}.
Recursion in arithmetic
- Write a function sum that sums up the integers
from 1 to n, i.e. it computes 1 + 2 + .. + n. For example
(sum 4) => 10.
- Write a function sum-squares that sums up the squares
of the integers from 1 to n, i.e. it computes 1^2 + 2^2 + .. + n^2.
For example
(sum-squares 4) => 30.
Recursion on words
Write a function prefix that takes as input an integer
and a word and returns as many letters starting from the beginning
of the word as the integer indicates. For example
(prefix 3 'Arthur) => art and
(prefix 0 'Arthur) => "". You may assume
that the integer is at most the length of the word.