Docsity

Sample Final Questions for CS 421, Fall 2006, Exams of Computer Science

Toggle sidebar
bg1
CS 421, Fall 2006
Sample Final Questions
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. 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;;
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)) in foldright (+) [2;3;4] 0
4. 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 functions which:
a. and
b. or:
c. not:
d. eq:
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.
i) a*∨b*∨c*
1
pf3

Partial preview of the text

Download Sample Final Questions for CS 421, Fall 2006 and more Exams Computer Science in PDF only on Docsity!

CS 421, Fall 2006

Sample Final Questions

  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. 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;;

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)) in foldright (+) [2;3;4] 0

  1. 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 functions which:

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

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

  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.) b. Assume we have an OCaml type exp for our language expressions, type comm for language commands with constructors IfThenElse: exp * comm * comm and RepeatUntil: comm * exp * comm, a type value with constructors TrueVal and FalseVal corresponding to true and false, a type mem associating variables with values, and type eval_exp_result = Inter_exp of (exp * mem) | Final of value type eval_comm_result = Mid of (exp * mem) | Dome of mem Write Ocsml clauses for a function eval comm : (expmem) -> eval comm result for the case of IfThenElse and RepeatUntil. You may assume that all other needed clauses of eval comm have been defined, as well as the function eval exp: (expmem) -> eval exp result.
  2. Assume you are given the OCaml types exp, bool exp and comm with (partially given) type definitions:

type comm = ... | If of (bool_exp * comm * comm) | ... type bool_exp = True_exp | False_exp | ...