sample questions for final -...

Info icon This 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]
Image of page 1

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

View Full Document Right 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]
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 the list of elements appearing in the nested list in order.
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern