




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: Progrmg Languages & Compilers; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2005;
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Answer: let rec every p l = match l with [] -> true | x::xs -> p x && every p xs let not_divides n q = not(n mod q = 0) let rec get_primes n = match n with 0 -> [] | 1 -> [] | _ -> let primes = get_primes (n-1) in if every (not_divides n) primes then n::primes else primes
Answer: let rec largest_aux lgst list = match list with [] -> lgst | x :: xs -> match lgst with None -> largest_aux (Some x) xs | Some l -> largest_aux (if l > x then lgst else (Some x)) xs
let largest = largest_aux None
(* I would also accept *) let largest list = List.fold_left (fun lgst x -> match lgst with None -> Some x | Some l -> if l > x then lgst else Some x) None list
let first lst = match lst with | a:: aa -> a;;
let rest lst = match lst with | [] -> [] | a:: aa -> aa;;
Answer: first : α list → α rest : α list → α list
Use these types to derive the types for:
b. let rec foldright f lst z = if lst == [] then z else (f (first lst) (foldright f (rest lst) z)) Answer: Because of space constraints the proof tree has been broken up into two parts:
Γ′ `f: α → β → β
Γ′ first: α list → α Γ′lst: α list Γ′ first lst: α Γ′f (first lst): β → β
Γ′ foldright: (α → β → β) → (α list) → β → β Γ′f: (α → β → β) Γ′ `foldright f: (α list) → β → β
Γ′ rest: α list → α list Γ′lst: α list Γ′ rest lst: α list Γ′foldright f (rest lst): β → β Γ′ z: β Γ′foldright f (rest lst) z: β Γ′ `f (first lst) (foldright f (rest lst) z): β
Γ′ lst: α list Γ′ [] : α list Γ′ lst == []:bool Γ′z: β
See above proof tree. Γ′ f (first lst) (foldright f (rest lst) z): β Γ′if lst == [] then z else (f (first lst) (foldright f (rest lst) z)): β Γ `let rec foldright f lst z = if lst == [] then z else (f (first lst) (foldright f (rest lst) z)): (α → β → β) → (α list) → β → β where Γ = { first : α list → α, rest : α list → α list }, and Γ′^ = Γ∪{foldright : (α → β → β) → (α list) → β → β, f : α → β → β, lst : α list, z : β}.
c. foldright (+) [2;3;4] 0
Answer:
Γ′′^ foldright: (α → β → β) → (α list) → β → β Γ′′^(+): int → int → int Γ′′^ foldright (+): int list → int → int Γ′′^[2;3;4]: int list Γ′′^ foldright (+) [2;3;4]: int → int Γ′′^ 0 : int Γ′′^ `foldright (+) [2;3;4] 0: int where Γ′^ = Γ ∪ {foldright : (α → β → β) → (α list) → β → β}.
a. Assuming Call by Name (Lazy Evaluation) Answer: With Call by Name (Lazy Evaluation): (λx.λy.yz)((λx.xxx)(λx.xx)) − β → (λy.yz)
Answer:
a. Give an example of a string for which this grammar has two different parse trees, and give their parse trees. b. Give the two leftmost derivations corresponding to those parse trees.
Answer:
a. “bab” b. S → A a B → b a B → b a b S → B a A → b a A → b a b
Answer: reg ::= a|b|reg ∨ reg|reg reg|reg∗ Atom ::= a|b|(RegExp) Star ::= Atom|Star * Concat ::= Star|StarConcat RegExp ::= Concat|Concat ∨ RegExp
type tokens = A_tk | B_tk | LParen | RParen | Star_tk | Or_tk type atom = A | B | Paren of regexp and star = Atom of atom | Star of star and concat = StarAsConcat of star | Concat of (star * concat) and regexp = ConcatAsRegExp of concat | Choice of (concat * regexp)
let rec mk_star (tree, tokens) = match tokens with Star_tk::more_toks -> mk_star (Star tree, more_toks) | _ -> (tree, tokens)
(* Not done yet *) let rec atom tokens = match tokens with (A_tk::rem_toks) -> (Some A,rem_toks) | (B_tk::rem_toks) -> (Some B,rem_toks) | (LParen::more_toks) -> (match regexp more_toks with (Some tree, RParen::rem_toks) -> (Some(Paren tree),rem_toks) | (, rem_toks) -> (None, rem_toks)) | _ -> (None, tokens) and star tokens = match atom tokens with (Some tree, rem_toks) -> (match mk_star (Atom tree, rem_toks) with (stree, toks) -> (Some stree, toks)) | (None, rem_toks) -> (None, rem_toks) and concat tokens = match star tokens with (Some tree, rem_toks) -> (match rem_toks with A_tk:: -> (match concat rem_toks with (Some tree1, more_toks) -> (Some(Concat(tree,tree1)), more_toks) | (None, more_toks) -> (None, more_toks)) | B_tk::_ -> (match concat rem_toks with (Some tree1, more_toks) -> (Some(Concat(tree,tree1)), more_toks) | (None, more_toks) -> (None, more_toks)) | LParen::_ -> (match concat rem_toks with (Some tree1, more_toks) -> (Some(Concat(tree,tree1)), more_toks) | (None, more_toks) -> (None, more_toks)) | _ -> (Some (StarAsConcat tree), rem_toks)) | (None, rem_toks) -> (None, rem_toks) and regexp tokens = match concat tokens with (Some tree, more_tokens) ->
Answer: delay : (unit -> ’a) -> unit -> ’a force : (unit -> ’a) -> ’a
b. Using the above constructions, but not the Lazy module from Ocaml, implement in Ocaml a type of ’a lazy list. Answer: type ’a lazy_list = Nil | Cons of (’a * (unit -> ’a lazy_list));;
c. Implement the function take: int -> ’a lazy list -> ’a list which returns the first n ele- ments of the lazy list. Answer: let rec take n llst = match n,llst with | ,Nil -> [] | 0, -> [] | _,(Cons(x,xs)) -> x :: take (n-1) (force xs);;
d. Create an infinite lazy list whose elements are the successive even numbers starting with 0. Answer: let rec map f l = match l with Nil -> Nil | Cons (x,xs) -> Cons (f x, delay (fun () -> map f (force xs))) let rec evens = Cons(0, (fun () -> map ((+) 2) evens))
Result: 1
Answer: let rec dividek n lst k = let rec dividek_aux n list inck = match list with [] -> inck n | 0::xs -> k 0 | x::xs -> dividek_aux n xs (fun r -> inck (r/x)) in dividek_aux n lst k
a. callcc (fun k -> throw k (10 + 15 + 20 + 25));;
Answer: 70
b. callcc (fun k -> throw k (10 + 15) + 20 + 25);; Answer: 25
c. callcc (fun k -> (10 + 15 + 20 + 25));; Answer: 70
d. Using callcc and throw, reimplement the following code for find : (’a -> bool) -> ’a list -> ’a option without using exceptions: exception NotPresent let find p lst = let rec find_aux p lst = match lst with [] -> raise NotPresent | x :: xs -> if p x then x else find_aux p xs in try Some (find_aux p lst) with NotPresent -> None
Answer: let find test list = callcc (fun empty_k -> let rec find_aux test list = match list with [] -> throw empty_k None | x :: xs -> if test x then Some x else find_aux test xs in find_aux test list)