sample questions for final

sample questions for final -...

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

View Full Document Right Arrow Icon
http://www.cs.ucsd.edu/classes/fa06/cse130/shw/sample-final-questions.txt CS 130 Fall 06: 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 http://www.cs.ucsd.edu/classes/fa06/cse130/shw/sample-final-questions.txt (1 of 12) [2/13/2008 5:20:32 PM]
Background image of page 1

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

View Full DocumentRight Arrow Icon
http://www.cs.ucsd.edu/classes/fa06/cse130/shw/sample-final-questions.txt 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 (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) http://www.cs.ucsd.edu/classes/fa06/cse130/shw/sample-final-questions.txt (2 of 12) [2/13/2008 5:20:32 PM]
Background image of page 2
http://www.cs.ucsd.edu/classes/fa06/cse130/shw/sample-final-questions.txt (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]". (b) Write a function flatten: 'a nlist -> 'a list that returns
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.

Page1 / 12

sample questions for final -...

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