cs342 Old Midterm
cs342 Old Midterm
Student No:
1. The main programming language used in the field of Numerical Analysis is:
3. Which of the following languages does not natively support garbage collection?
1
Name:
Student No:
10. Which of these Scheme inputs produces a value most similar to a C array?
(A) ’(.1 .2 .3) (B) (list .1 .2 .3) (C) #(.1 .2 .3) (D) (1 . 2 . 3)
11. Which of the following gives a result different from the others in Scheme?
(lambda (m n)
(let ((k (- m n)) (h (* m n)))
(+ h k x)))
14. In Scheme, which of the following never evaluates all of its subexpressions:
(A) (and a b #\f c d e)
(B) (or a b #\f c d e)
(C) (and a b #f c d e)
(D) (or a b #f c d e)
2
Name:
Student No:
Question II.1
(a) Define a function in Scheme to examine a list and count the number of elements that are
equal to 42.
(b) Write a Scheme function spread that takes a list of unary functions f1 , ..., fn , and a single
value v, and returns the list of results of calling fi on v:
For example
(c) Write a function curried-map that takes a unary function f as its only parameter and
returns a function that maps f on to any list. E.g.
Question II.2
This question concerns the meaning of let* in Scheme.
(b) Rewrite the same expression as an equivalent expression using one or more lambdas.
(c) Write a Scheme macro to implement let* in terms of lambda. Your solution must work
for a let* with any number of bound variables.
3
Name:
Student No:
Question II.3
This question concerns scoping mechanims. Consider the following program in pseudocode for
a block-structured language:
procedure outer(i, j) {
procedure inner(i) {
printf("Inner %d %d\n", i, j);
}
procedure middle(i, j) {
printf("Middle %d %d\n", i, j);
call inner(10);
}
In this pseudo-code, procedure is used to define procedures, and call is used to invoke proce-
dures.
(a) Which of the variables i and j are bound and which are free in each of outer, middle and
inner? Explain briefly, with a couple of sentences.
(b) What does this program print, assuming the language uses static (lexical) scoping? Explain
briefly, with a couple of sentences.
(c) What does this program print, assuming the language uses dynamic scoping? Explain
briefly, with a couple of sentences.
Question II.4
Suppose you were stranded on a desert island with a bicycle-powered Centrino-based PC with a
text editor and a C compiler, including source code. The source code is well structured, without
a clear separation of machine-dependent and machine-independent parts.
One day a crate containing a solar-powered top-of-the line iMac washes up on shore. This
machine uses a PowerPC processor that is incompatible with the Centrino. The iMac has a text
editor and all the documentation you would ever want, but no compiler. You are able to move
files back and forth between the machines using a USB drive.
Describe how would you go about getting a C compiler running on the iMac. Outline your steps
clearly in point form. You have no off-shore network connection from which you can download
anything else – otherwise you would have E-mailed for help already.