Docsity

CS 421 Fall 2008 Final Exam Questions and Instructions, Assignments of Computer Science

Toggle sidebar
bg1
CS 421, Fall 2008
Sample Final Questions
You should review the questions from the sample midterm exams, the real midterm exams, and the
homework, as well as these question.
1. Write a function get primes : int -> int list that returns the list of primes 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.
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.
3. Write a function dividek n lst k, using Continuation Passing Style (CPS), that divides nsuccessively
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 kimmediately, without doing any divisions.
# dividek 6 [1;3;2] report;;
Result: 1
- : unit = ()
4. a. Give the types for following functions (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;;
b. Use these types (i.e., start in an environment with these identifiers bound to these types) to give a
type derivation for:
let rec foldright f lst z =
if lst = [] then z
else (f (first lst) (foldright f (rest lst) z))
in foldright (+) [2;3;4] 0
5. 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.
1
pf3

Partial preview of the text

Download CS 421 Fall 2008 Final Exam Questions and Instructions and more Assignments Computer Science in PDF only on Docsity!

CS 421, Fall 2008

Sample Final Questions

You should review the questions from the sample midterm exams, the real midterm exams, and the homework, as well as these question.

  1. Write a function get primes : int -> int list that returns the list of primes 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.
  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.
  3. 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 = ()
  1. a. Give the types for following functions (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;;

b. Use these types (i.e., start in an environment with these identifiers bound to these types) to give a type derivation for: let rec foldright f lst z = if lst = [] then z else (f (first lst) (foldright f (rest lst) z)) in foldright (+) [2;3;4] 0

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

  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 Give an example of a string for which this grammar has two different parse trees, and give their parse trees.

  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.
  2. Use the following encodings of true, false and if to define lambda terms and, or, not, eq which respectively return booleans corresponding to conjuction, disjunction, negation, and test for equality.

true = λ a b. a false = λ a b. b if = λ c t e.c t e

Define the following functions as lambda terms:

a. and: b. or: c. not: d. eq:

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

a. Assuming Call by Name (Lazy Evaluation) b. Assuming Call by Value (Eager Evaluation) c. To full αβ-normal form.

  1. a. Write the transition semantics rules for if then else and repeat until. (A repeat until executes the code in the body of the loop and then checks the condition, exiting if the condition is true.)