sample-final-questions

sample-final-questions - CS 130 Fall 07: Sample Questions...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 130 Fall 07: Sample Questions for Final _________________________________________________________________________ _________________________________________________________________________ Problem 1: --------- For each ML function given below, if the function is well typed, then write down its type, otherwise explain in less than two lines why SML rejects the function. (a) let rec rev l = match l with [] -> [] | (h::t) -> (rev t)::h (b) let rec f l = match l with [] -> [] | (h::t) -> (List.hd h)::t (c) let rec f (tup,l) = match l with [] -> 0 | (h::t) -> tup.h + f (tup,t) (d) let rec f (x,y) = if x then (List.hd y) + 20 else if not x then 0 else y (e) let rec f l = match l with [] -> [] | f (h::t) -> h @ (flatten t) _________________________________________________________________________ Problem 2: --------- Which of the functions below are tail recursive ? Also, for each function, write down its type. For the ones that are not, write down tail recursive versions that achieve the same task. (a) let rec map f l = match l with [] -> [] | (h::t) -> (f h) :: (map f t) (b) let rec fold f b l = match l with [] -> b | (h::t) -> fold f (f (h,b)) t
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(c) let rec len l = match l with [] -> 0 | (h::t) -> 1 + len t (d) let rec f (n,l) = match l with [] -> n | (n,h::t) = if (h>n) then f (h,t) else f(n,t) Solution: run Ocaml to get types. -------- (a) Not tail recursive. let map f l = let rec helper (acc,l') = match l' with [] -> acc | (acc,h::t) = helper((f h)::acc,t) in List.rev(helper([],l)) (b) Tail recursive. (c) Not tail recursive. let len l = let rec helper (n,l') = match l' with [] -> n | helper (n,_::t) = helper(s+1,t) in helper(0,t) (d) Tail recursive. _________________________________________________________________________ Problem 3: --------- The following datatype can be used to represent arbitrarily nested lists. type 'a nlist = Nil | Cons of 'a elt * 'a nlist and 'a elt = E of 'a | NL of 'a nlist The list [1,2] could be represented as Cons(E 1,Cons(E 2,Nil)), the list [[1],2] as Cons(NL(Cons((E 1),Nil)),Cons(E 2,Nil)) and the list [[[],[1]],2] as Cons(NL(Cons(NL Nil,Cons (E 1,Nil))), Cons(E 2,Nil)). Using only pattern-matching (i.e. no if-then-else expressions): (a) Write a function: nlist_toString: ('a -> string) -> 'a nlist -> string such that if nl is bound to Cons(NL(Cons(NL Nil,Cons (E 1,Nil))), Cons(E 2,Nil)), then (nlist_toString Int.toString nl) evaluates to: "[[[],[1]],2]".
Background image of page 2
(b) Write a function flatten: 'a nlist -> 'a list that returns the list of elements appearing in the nested list in order. Thus when applied to any of the lists shown above, flatten
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/16/2010 for the course CSE 130 taught by Professor Jhala during the Winter '06 term at UCSD.

Page1 / 10

sample-final-questions - CS 130 Fall 07: Sample Questions...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online