HW1
Due Date: Friday, September 18 at 11:59 PM
- Upload your submission to Gradescope, starting from this starter file.
Each function in this file is filled in with
..., which throws an error about the code being unimplemented; your job is to fill in each function. (The Gradescope assignment will appear shortly.)
You should upload only a single Lean file beginning with the following two lines:
import Lait
#lait
-
Be sure to check that your submission is valid Lait code that type checks; if it is not, you will receive 0 points for that problem.
-
Your programs will be evaluated based on a number of tests. Gradescope will show you some tests when you submit, but not all of them. However, all of the tests we run will be testing the program’s specification, which will be unambiguous. You should test your code thoroughly before submitting.
-
For each problem, you should include at least 3 passing test cases different than the ones given below.
1. Min (10 points)
Implement a function min that takes as input two numbers and outputs which one is smaller.
def min (i : Int) (j : Int) : Int := ...
Example tests:
#test min 0 1 === 0
2. Sorted (10 points)
Implement a function sorted? of type List<Int> -> Bool which
returns true if and only if the list is sorted from least to greatest.
def sorted? (xs : List<Int>) : Bool := ...
Example tests:
#test sorted? [1, 2, 3] === true
#test sorted? [3, 2, 1] === false
3. Summing a Tree (10 points)
For this problem, we will use the following type for a binary tree:
type Tree := | Leaf (val : Int) | Node (left : Tree) (right : Tree)
Implement the function sum_tree of type Tree -> Int which returns the sum of all the integers in the tree.
def sum_tree (t : Tree) : Int := ...
Example test:
#test sum_tree (Node (Leaf 10) (Leaf 20)) === 30
4. Right Linear (10 points)
A binary tree is right linear iff, for every node in the tree, the left child is a leaf. For example, the following tree is right-linear:
/\
/ \
1 /\
2 3
The following tree is not:
/\
/\ \
1 2 \
3
Implement a function right_linear? of type Tree -> Bool
that returns true if and only if the tree is right linear.
def right_linear? (t : Tree) : Bool := ...
Example tests:
#test right_linear? (Node (Leaf 1) (Node (Leaf 2) (Leaf 3))) === true
5. DFS List (10 points)
A depth-first left-first traversal of a tree traverses the tree in depth-first order starting with the left child of each node. Consider this tree:
/\
/ 3
/\
1 2
For the above tree, a depth-first left-first traversal explores leaf 1, then 2, then 3.
Implement a function dfs_list of type Tree -> List<Int> that returns a
list of all the leaf values traversed according to depth-first left-first order.
def dfs_list (t : Tree) : List<Int> := ...
6. Subst (10 points)
Implement a function subst of type Tree -> Int -> Int -> Tree.
Given arguments subst t old n, it
takes a tree t and two numbers old and n and returns a tree that
results from substituting all leaves of value old with n. This is like a “find
and replace” for trees.
def subst (t : Tree) (old : Int) (n : Int) : Tree := ...
7. Multi-Subst (20 points)
Now we want to perform multiple substitutions at once. We will keep track of all the substitutions in a map.
The following definitions are built into Lait’s standard library:
- A type
Map<k, v>of maps with keys of typek, and values of typev. Map.lookup : Map<k, v> -> k -> Option<v>
You can find more information about maps in the Lait book.
Implement the function multi_subst of type Tree -> Map<Int,Int> -> Tree
which takes a tree t and a map m, and
returns a new tree that results from performing all the substitutions specified
by m. If a leaf’s value is not found in the map, leave that
leaf unchanged.
def multi_subst (t : Tree) (m : Map<Int,Int>) : Tree := ...
8. Filter_false (10 points)
Implement a function filter_false of type List<Int> -> (Int -> Bool) -> List<Int>
that takes a list of numbers xs and a function f as input, and returns a new list
that contains only those elements in xs for which f returns false.
def filter_false (xs : List<Int>) (f : Int -> Bool) : List<Int> := ...
Some example tests:
#test filter_false [1, 2, 2, 3] (fun x => x == 3) === [1, 2, 2]
#test filter_false [1, 2, 2, 3] (fun x => x == 2) === [1, 3]