Docsity

Sample Final Questions for OCaml Programming Course, Exams of Computer Science

Toggle sidebar
bg1
Sample Final Questions
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 div and rem. 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 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;;
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))
c. foldright (+) [2;3;4] 0
4. Reduce the following expression: (λxλy .yz)((λx.xxx)(λx.xx))
a. Assuming Call by Name (Eager Evaluation)
b. Assuming Call by Value (Lazy Evaluation)
5. Using the following encodings of true, false and if to define lambda terms and, or, not, eq which
return, respectively, 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
pf3

Partial preview of the text

Download Sample Final Questions for OCaml Programming Course and more Exams Computer Science in PDF only on Docsity!

Sample Final Questions

  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 div and rem. 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 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;;

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

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

a. Assuming Call by Name (Eager Evaluation) b. Assuming Call by Value (Lazy Evaluation)

  1. Using the following encodings of true, false and if to define lambda terms and, or, not, eq which return, respectively, 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. Give the value of the following expression:

let a = 3 in let p x = x * a in let a = 5 in a + (p 7);;

a. assuming static scope (a.k.a. lexical scope): b. assuming dynamic scope:

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

  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. 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.) Assuming a datatype for expressions in Ocaml has been declared, with constructors IfThenElse: exp -> exp -> exp -> exp and RepeatUntil: exp -> exp -> exp, write Ocsml clauses for a function eval: exp -> value. You may assume that all other needed clauses of eval have been defined, abd the TrueVal and FalseVal are the values corresponding to true and false.