



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




fold_left.]:
(a) repeat_until: ('a -> bool) -> ('a -> 'a) -> 'a -> 'a. where repeat_until p f x = x, if p x, or f x if
p (f x), or f (f x) if p (f (f x)), etc.
(b) sift: (‘a -> bool) -> ‘a list -> ‘a list * ‘a list. sift p lis splits lis into a pair of lists (lis1, lis2), with lis
containing those elements of lis that satisfy p and lis2 the others.
(c) Write sift using fold_right. Specifically, define sift_base and sift_rec so that
fold_right (sift_rec p) lis sift_base = sift p lis
(d) sublist: 'a list -> int -> int -> 'a list. This function, when applied as sublist lst lo hi, returns the elements
of the list lst that are positioned between lo and hi (indexing from zero). The function should return empty set when lo > hi, or when lo is out of the bounds of the input list; if hi is out of bounds, just return
the elements from lo to the end of the list.
(e) Write "map" using "fold_left".
(f) app_all [f1;f2;…] a = [f1 a; f2 a; …]. Define app_all and say what its type is.
(g) compose_all [f1;f2;…] a = f1 (f2 (… (fn a)…)). Define compose_all and say what its type is.
a. a(b | c)a* b. (a | b)ca* c. abc(b | c)a*
Give an example of a string that shows the difference (i.e. either is accepted by the odd regular expression and not by the others, or vice versa).
E → T + E | T T → T * P | P P → id | ( E )
(a) Give the parse tree for: x + (u * v * w)
(b) What associativity of + is implied by this grammar? How about *?
(c) Give a new grammar, based on this one, that includes operator ^ (exponentiation), with higher
precedence than *. ^ is right-associative.
a) S ::= M + S M ::= M * id | id
b) E ::= if B then A else A | if B then A
A ::= id + A | 1 | 0 B ::= true | false
c) L ::= '(' A A ::= id B | ')' B ::= ',' id B | ')'
x = y x = n x = &y (get address of variable y) x = a op b, where a and b are names or constants, and op is any arithmetic, comparison, or boolean operator JUMP label CJUMP x, Lt, Lf (jump to Lt if x is true, Lf otherwise) x = LOADIND y (get value in location contained in y) ERROR (return an error)
You may use any of the following translation schemes, which were discussed in class:
[S] = instruction list to compute statement S [e] = pair containing instruction list to compute expression e and variable name giving location of result
let rec mkvec m f = if m=0 then [] else f() :: mkvec (m-1) f;; let binom n k = let table = mkvec (n+1) (fun () -> mkvec (k+1) (fun () -> ref (-1))) in let rec binom' n k = let v = nth (nth table n) k in if !v != - then !v else (if k=0 or k=n then v := 1 else v := (binom' (n-1) (k-1) + binom' (n-1) k); !v) in binom' n k;;
int a = n-1; int x = 1, y = 1; while (a > 0){ int t = x; x = y; y = y+t; a = a-1; }