This Midterm is worth 50 points. Each point represents a minute of time. Use the point totals as a guide for budgeting your time. The exam is closed-book, closed-notes, open-mind.
Page | Possible | Score |
---|---|---|
2 | 15 | |
3 | 7 | |
4 | 14 | |
5 | 14 | |
Total | 50 |
For the purposes of this exam, assume the following functions are
defined for you. We just show the "prototypes" of the functions
below:
;; map the function f over the given list ;; return the list consisting of f applied to each element of a-list (map f a-list) ;; reduce the given list, given a reducing function f and a base value (reduce f base a-list) ;; return the length of the given list (length a-list) ;; return a list which is the concatenation of the given lists (append listA listB) ;; return a list which is the reverse of the given list (reverse a-list)
(car (+ 1 2) (* 3 4))
(cons (+ 1 2) '(- 3 4))
(append '(1 2) (reverse '(3 4))))
(map (lambda (x) (>= x 10)) '(11 10 9 5 15 6 0))
(list 3 (cdr 1 2))
TRUE / FALSE When garbage-collecting large objects, copy-collectors are
preferable to mark-sweep collectors.
TRUE / FALSE Mark-sweep collectors tend to fragment heap memory.
TRUE / FALSE Scheme is dynamically typed, meaning that it cannot
be strongly typed.
TRUE / FALSE C is an example of a statically typed language.
TRUE / FALSE In a C program, space for local variables is allocated in
the static region of program memory.
(3.) [5 points] Short Answer. Answer ONLY one of the below. Correct, concise answers will be rewarded.
(list 1 2 3)
I would answer:(1 2 3)
4.1)
'(((foo)) bar)4.2)
(list (cons (+ 1 2) ()) 'foo)4.3)
(let* ((x '(3 4)) (y (cons '(1 2) x)) (z '(4 5 6))) (list (+ (car x) (car (cdr y))) (+ (car (cdr x)) (car (cdr (cdr z))))))
char-count
, which takes list of symbols. It returns a
list of the lengths of the symbols in the original list. The function
must be written recursively and cannot make use of any of Scheme's
higher-order functions such as map
. You should use the
built-in functions symbol->string
and
string-length
to determine the length of a symbol. Here
are some examples:
(symbol->string 'foobar) => "foobar" (string-length (symbol->string 'foobar)) => 6 (char-count '(Hello there my name is Bob)) => (5 5 2 4 2 3)(6.) [7 points] Write
count-chars
again, this time using
map
. You must use a lambda
expression (do not
define extraneous helper functions).
stats
that takes a "document" and returns a
list containing three items: total number of characters, total number
of words, and total number of lines. For example, the below
"document" contains 33 characters, 9 words, and 4 sentences.
HINTS: If you leverage char-count
, as well as
reduce
, append
, and length
,
this function is quick to write! If you couldn't write
char-count
, assume it's defined for you.
(stats '((Dear Sue) (I am learning Scheme) (See ya) (Bill))) => (33 9 4)(8.) [7 points] Write a function called
map2
, that
takes three arguments: a function (which takes two arguments) and two
lists. map2
should return a list that consists of the
provided function applied to the corresponding elements of the two
lists. Example:
(map2 (lambda (x y) (+ 1 (* x y))) '(1 2 3) '(4 5 6)) => (5 11 19)