






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







(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.
(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.
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:
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)
(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
{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.