This Midterm is worth 60 points. The exam is closed-book, closed-notes, open-mind.
Page | Possible | Score |
---|---|---|
2 | 10 | |
3 | 10 | |
4 | 10 | |
5 | 10 | |
6 | 10 | |
7 | 10 | |
Total | 60 |
For the purposes of this exam, assume the following functions are
defined for you.
val map = fn : ('a -> 'b) * 'a list -> 'b list val reduce = fn : ('a * 'b -> 'b) * 'b * 'a list -> 'b (* Return a list of elements that pass the provided predicate test. *) val filter = fn : ('a -> bool) * 'a list -> 'a list (* Return the greater of two provided integers. *) val Int.max = fn : int * int -> int (* Take a string and return it as a list of characters. *) val explode = fn : string -> char list
fun compose(f, g, x) = f(g(x)); fun average(x, y) = x + y / 2.0; fun circumference(r) = 2 * r * 3.14; fun bar(a, b, c) = [a, b + hd(c)]; fun why(nil) = nil | why((a,b)::xs) = (a+b) :: why(xs); fun everyOther(nil) = nil | everyOther(f, a::b::xs) = f(a) :: everyOther(xs); fun curriedArea width height = width * height; fun weird x z = [x, tl(z)];
3.1)(a, [b, c], d) and ("hello", [7, 8], [1, 2])
[a, b::c] and [[1, 2], [3]]
concatList
that takes a list of strings and concatenates them together. Recall
that the ^ operator can be used to concatenate two strings.
> concatList(["hello" "goobye" "foobar"]); val it = "hellogoodbyefoobar";(5.) [7 points] Write a function called
search
that takes two strings, and returns true iff the first string is found
somewhere within the second string. Hint: Use the built-in function
explode
to turn strings into lists of characters. The expected
type profile and some examples are shown below:
> search; val it = fn : string * string -> bool > search("bok", "hoboken"); val it = true; > search("art", "barbar") val it = false;
structure BankAccount : sig (* An Account is a tuple of a name and a balance *) datatype Account = Account of string * real exception NoFunds (* Given an Account, return its balance. *) val balance : Account -> real (* Given an Account, return the owner's name. *) val name : Account -> string (* Create a new Account with the given owner, and zero funds. *) val newAccount : string -> Account (* Deposit this amount into the given Account. *) val deposit : real * Account -> Account (* Withdraw this amount from the given Account. Raises NoFunds if there is not enough money. *) val withdraw : real * Account -> Account end
Tree
which can be used to represent expression trees.
Your job is to write the following:
tree
) which
represents the expression 3 + 4 * 7. (For full credit, the tree
should represent the fact that * has higher precedence than +.)
eval
, which evaluates an
expression tree. It takes a Tree and returns an integer.
height
, which takes a Tree and
returns an integer that is the height of that tree.
datatype Tree = Leaf of int | Add of Tree * Tree | Sub of Tree * Tree | Mult of Tree * Tree | Div of Tree * Tree;(8.) [3 points] Give just the the definition for a generalized tree datatype. Nodes in a generalized tree may have any number of children. The labels/elements of your tree should be of any type.