Sample Final Exam Questions with Answer - Programming Language | CS 421, Exams of Computer Science

Material Type: Exam; Class: Progrmg Languages & Compilers; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2005;

Typology: Exams

Pre 2010

Uploaded on 03/16/2009

koofers-user-nop
koofers-user-nop 🇺🇸

8 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 421, Fall 2005
Sample Final Questions & Answers Incomplete
1. Write a function get primes : int -> int list that returns the list of integers less than or equal
the input. You may use the built-in functions /and mod. You will probably want to write one or more
auxiliary functions. Remember that 0 and 1 are not prime.
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
2. Write a tail-recursive function largest: int list -> int option that returns Some of the largest
element in a list if there is one, or else None if the list is empty.
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
3. a. Give the types for following expressions (you don’t have to derive them):
let first lst = match lst with
| a:: aa -> a;;
let rest lst = match lst with
| [] -> []
| a:: aa -> aa;;
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Sample Final Exam Questions with Answer - Programming Language | CS 421 and more Exams Computer Science in PDF only on Docsity!

CS 421, Fall 2005

Sample Final Questions & Answers Incomplete

  1. Write a function get primes : int -> int list that returns the list of integers less than or equal the input. You may use the built-in functions / and mod. You will probably want to write one or more auxiliary functions. Remember that 0 and 1 are not prime.

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

  1. Write a tail-recursive function largest: int list -> int option that returns Some of the largest element in a list if there is one, or else None if the list is empty.

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

  1. a. Give the types for following expressions (you don’t have to derive them):

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) → β → β}.

  1. Reduce the following expression: (λxλy.yz)((λx.xxx)(λx.xx))

a. Assuming Call by Name (Lazy Evaluation) Answer: With Call by Name (Lazy Evaluation): (λx.λy.yz)((λx.xxx)(λx.xx)) − β → (λy.yz)

  1. a. For each of the regular expressions below (over the alphabet {a,b,c}), draw a nondeterministic finite state automaton that accepts exactly the same set of strings as the given regular expression. i) a∨b∨c* ii) ((aba∨bab)c(aa∨bb))* iii) (ab)(c∨)(ba) b. For each nondeterministic finite state automaton given for part a, give a corresponding deterministic finite state automaton that excepts exactly the same set of strings.

Answer:

  1. Consider the following ambiguous grammar (Capitals are nonterminals, lowercase are terminals): S → A a B | B a A A → b | c B → a | b

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

  1. Write a unambiguous grammar for regular expressions over the alphabet {a, b}. The Kleene star binds most tightly, followed by concatenation, and then choice. Here we will have concatenation and choice associate to the right. Write an Ocaml datatype corresponding to the tokens for parsing regular expres- sions, and one for capturing the abstract syntax trees corresponding to parses given by your grammar. Write a recursive descent parser for regular expressions, producing an option (Some) of an abstract syntax tree if a parse exists, or None otherwise.

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))

  1. Write a function dividek n lst k, using Continuation Passing Style (CPS), that divides n successively by every number in the list, starting from the last element in the list. If a zero is encountered in the list, the function should pass 0 to k immediately, without doing any divisions.

dividek 6 [1;3;2] report;;

Result: 1

  • : unit = ()

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

  1. Assuming Ocaml had callcc and throw implemented as discussed in class (and as implemented in SML/NJ), what would be the result of the following:

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)