Docsity

CS421 Summer 2008 Midterm Study: OCaml, Higher-Order Functions, Regex, DFAs, NFAs, CFGs, Exams of Computer Science

Toggle sidebar
bg1
CS421 Summer 2008 Midterm Study Guide
CS 421 — Programming Languages and Compilers
Summer Term 2008
1. OCaml and Higher-Order Functions:
(a) Using just fold, write a function max that finds the largest element in a list of numbers.
Consider this to be 0 for an empty list.
(b) Say you are given the following definition of lists:
type ‘a mylist =Empty |Cons of ‘a∗‘a mylist
Write a function myMap that takes a function and maps it over the elements of a mylist.
University of Illinois at Urbana-Champaign Netid:
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS421 Summer 2008 Midterm Study: OCaml, Higher-Order Functions, Regex, DFAs, NFAs, CFGs and more Exams Computer Science in PDF only on Docsity!

CS421 Summer 2008 Midterm Study Guide

CS 421 — Programming Languages and Compilers

Summer Term 2008

  1. OCaml and Higher-Order Functions:

(a) Using just fold, write a function max that finds the largest element in a list of numbers. Consider this to be 0 for an empty list.

(b) Say you are given the following definition of lists: type ‘a mylist = Empty | Cons of ‘a ∗ ‘a mylist Write a function myMap that takes a function and maps it over the elements of a mylist.

(c) Say you are given the following definition of trees, with an arbitrary number of children: type ‘a mytree = Leaf of ‘a | Node of ‘a mytree list Write a function fold mytree that takes a function for the node case, a function for the leaf case, an identity, and a mytree, and returns the result of folding the functions over the tree. Note, you may want to think about mutually recursive functions here.

(d) Using fold mytree, write a function sum mytree that will sum the values of the leaves.

  1. DFAs and NFAs:

(a) Given the alphabet {x, y, z}, define an NFA that recognizes all strings ending in z that have at least one y

(b) Given the alphabet {a, b}, define a DFA that recognizes all strings of a’s and b’s that start and end with the same letter.

  1. Context-Free Grammars, Derivations, and Parse Trees: The following exercises make use of this grammar for arithmetic expressions. Here, the start symbol is E, id refers to iden- tifiers made up of one lowercase character (a, b, · · · , y, z), and num refers to any integer:

E → E + T T → T ∗ F F → id E → E − T T → T /F F → num E → T T → F F → (E)

(a) Show a leftmost derivation for the following term: y/ 3

(b) Show a rightmost derivation for the same term:

(c) Provide a parse tree for the same term:

  1. Type Derivations:

Note, type derivation rules are on the last page.

(a) Show the complete type derivation for the following term: (fun x −> if x = 0 then true else false) 3

(b) Show the complete type derivation for the following term:

let x = 5 in g x where g is assigned type g : int → string

(d) Show the complete type derivation for the following term:

let rec f = fun a −> (a < 9 ) in (if (f 4) then 3 else 5)

  1. Unification:

(a) Give a most general unifier for the following unification problem. Lower case letters (f, g, h, d) are constants or term constructors: specifically, f and g are term constructors with arity 1, h is a term constructor with arity 2, and d is a constant term with arity

  1. Letters α,β, and γ are variables. Show your work by listing the operation performed in each step of unification – decompose, orient, delete, eliminate – and the result of the step. If unification is not possible, work as far as possible and show where unification fails. If unification does not fail, show the final substitution, which should be a set of variable to term mappings.

{f(α) = f(g(γ)); γ = d; α = g(β); h(α, γ) = h(α, β)}

(b) Using the unifier discovered above, apply the substitution and show the final terms, which should be equalities and which should have no variables. If unification got stuck above, just write “unification failed” below.