



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Class: Progrmg Languages & Compilers; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Summer 2008;
Typology: Lab Reports
1 / 7
This page cannot be seen from the preview
Don't miss anything!




1.1 Fixed multifold example, clarified some questions.
1.0 Initial Release.
This MP is meant to give you practice writing and using higher order functions. The only functions you may use from the List module for this MP are List.map, List.fold right, List.fold left, List.hd, List.tl, and List.rev. You may, however, write helper functions. Upon completion of this MP, you should have the following skills:
Start with the file mp2.ml, provided in the .tgz grader package. Stubs are provided for each function mentioned below. You can add helper functions, either outside of these function definitions or inside as local definitions. Note that the stub contains the following at the top:
open Mp2common;;
This will include certain data types used in this MP for you. In order to #use "mp2.ml" in an interactive OCaml session, you must first download and compile mp2common.ml with
ocamlc -c mp2common.ml
Make sure that mp2.ml, mp2common.cmi, and mp2comon.cmo are in the same directory (you can also open Mp2common directly in a OCaml interactive session, once you’ve compiled it). Your solution file must have the line open Mp2common;; before your function definitions. Please don’t remove it! The problems are designed to be done sequentially, so you can always use the solution to an earlier problem in a later problem.
4 Problems
Write the following higher order functions. The first three are memory tests – you can of course look them up, but you should be able to write them from memory without doing so.
val map : (’a -> ’b) -> ’a list -> ’b list =
val fold_right : (’a -> ’b -> ’b) -> ’a list -> ’b -> ’b =
val fold_left : (’a -> ’b -> ’a) -> ’a -> ’b list -> ’a =
val exists : (’a -> bool) -> ’a list -> bool =
val exists_fold : (’a -> bool) -> ’a list -> bool =
val rsum : int list -> int list =
Recall from the lecture that we can write functions like map and fold can work over other datatypes, such as binary trees. Mp2common already defines this type for you:
type ’a btree = Node of ’a btree (^) * ’a btree | Leaf of ’a | Empty
val mapbtree : (’a -> ’b) -> ’a btree -> ’b btree =
val foldbtree : (’a -> ’a -> ’a) -> (’b -> ’a) -> ’b btree -> ’a -> ’a =
For this part, you may not use recursion, except that which is contained in the higher order functions you wrote above. All the solutions to these (except isSearch) are one line long. (Yours can be longer than that.)
val depth : ’a btree -> int =
val isBinary : ’a btree -> bool =
val flip : ’a btree -> ’a btree =
val isSearch : int btree -> bool =
Recall from lecture the datatype ’a labeled tree:
type ’a labeled_tree = LTreeNode of (’a (^) * ’a labeled_tree list);;
This type may be found in Mp2common.
let rec foldLabTree nodeFun nilCase consFun t= ... ;; val foldLabTree : (’a -> ’b -> ’c) -> ’b -> (’c -> ’b -> ’b) -> ’a Mp2common.labeled_tree -> ’c = val foldLabList : (’a -> ’b -> ’c) -> ’b -> (’c -> ’b -> ’b) -> ’a Mp2common.labeled_tree list -> ’b =
LTreeNode(5, [LTreeNode (3, []); LTreeNode (2, [LTreeNode (1, []); LTreeNode (7, [])]); LTreeNode (5, [])]);; val ltree : int labeled_tree = LTreeNode (5, [LTreeNode (3, []); LTreeNode (2, [LTreeNode (1, []); LTreeNode (7, [])]); LTreeNode (5, [])])
val ltree : (int (^) * string) labeled_tree = LTreeNode ((5, "a"), [LTreeNode ((3, "a"), []); LTreeNode ((2, "a"), [LTreeNode ((1, "a"), []); LTreeNode ((7, "a"), [])]); LTreeNode ((5, "a"), [])])
6 Handing In
Use the handin program on dcllnx?.ews.uiuc.edu (? being some machine number, which should not matter) as you did for MP1. See the instructions in the FAQ section of the webpage.