OCML代写 | CSE 595 – Homework I
这个作业是用OCML完成微积分相关的习题
CSE 595 – Homework I
1 λ Calculus
1. In the basic untyped lambda calculus, the boolean true is encoded as λx.λy.x (i.e., it takes in two (3)
arguments and returns the first), and false is encoded as λx.λy.y (i.e., takes in two arguments and
returns the second). These definitions of the boolean constants may seem strange, but they are designed
to work with the if-then-else expression. The if-then-else expression is defined as λx.λy.λz.((xy)z).
Verify that these definitions do, indeed, make sense, by evaluating the following:
(a) (((λx.λy.λz.((xy)z)(λu.λv.u))A)B)
(b) (((λx.λy.λz.((xy)z)(λu.λv.v))A)B)
2. Reduce the following λ-expressions to their fullest possible extent: (3)
(a) (λx.xx)(λy.yx)z
(b) (((λx.λy.(xy))(λy.y))w)
3. Given the definitions of true and false as in the very question, and not defined as λx.((x false) true), (4)
and or defined as λx.λy.((x true) y), prove the following:
(a) not (not true) = true
(b) or false true = true
2 Recursion and Higher-order Functions
In this section, you may not use any functions available in the OCaml library that already solves all or most
of the question. For example, OCaml provides a List.rev function, but you may not use that in this section.
1. Write a recursive function pow, which takes two integer parameters x and n, and returns x (5) n
. Also write a
function float pow, which does the same thing, but for x being a float. n is still a non-negative integer.
2. Write a function compress to remove consecutive duplicates from a list. (5)
# compress [“a”;”a”;”b”;”c”;”c”;”a”;”a”;”d”;”e”;”e”;”e”];;
– : string list = [“a”; “b”; “c”; “a”; “d”; “e”]
3. Write a function remove if of the type ‘a list -> (‘a -> bool) -> ‘a list, which takes a list and (5)
a predicate, and removes all the elements that satisfy the condition expressed in the predicate.
# remove_if [1;2;3;4;5] (fun x -> x mod 2 = 1);;
– : int list = [2; 4]
4. Some programming languages (like Python) allow us to quickly slice a list based on two integers i and (5)
j, to return the sublist from index i (inclusive) and j (not inclusive). We want such a slicing function
in OCaml as well.
Write a function slice as follows: given a list and two indices, i and j, extract the slice of the list
containing the elements from the i
th (inclusive) to the j
th (not inclusive) positions in the original list.
# slice [“a”;”b”;”c”;”d”;”e”;”f”;”g”;”h”] 2 6;;
– : string list = [“c”; “d”; “e”; “f”]
Invalid index arguments should be handled gracefully. For example,
# slice [“a”;”b”;”c”;”d”;”e”;”f”;”g”;”h”] 3 2;;
– : string list = []
# slice [“a”;”b”;”c”;”d”;”e”;”f”;”g”;”h”] 3 20;
– : string list = [“d”;”e”;”f”;”g”;”h”];
You do not, however, need to worry about handling negative indices.
5. Write a function equivs of the type (‘a -> ‘a -> bool) -> ‘a list -> ‘a list list, which par- (7)
titions a list into equivalence classes according to the equivalence function.
# equivs (=) [1;2;3;4];;
– : int list list = [[1];[2];[3];[4]]
# equivs (fun x y -> (=) (x mod 2) (y mod 2)) [1; 2; 3; 4; 5; 6; 7; 8];;
– : int list list = [[1; 3; 5; 7]; [2; 4; 6; 8]]
6. Goldbach’s conjecture states that every positive even number greater than 2 is the sum of two prime (7)
numbers. E.g., 18 = 5 + 13, or 42 = 19 + 23. It is one of the most famous conjectures in number theory.
It is unproven, but verified for all integers up to 4 × 1018. Write a function goldbachpair : int ->
int * int to find two prime numbers that sum up to a given even integer. The returned pair must have
a non-decreasing order.
# goldbachpair 10;; (* must return (3, 7) and not (7, 3) *)
– : int * int = (3, 7)
Note that the decomposition is not always unique. E.g., 10 can be written as 3+7 or as 5+5, so both
(3, 7) and (5, 5) are correct answers.
7. Write a function called equiv on, which takes three inputs: two functions f and g, and a list lst. It (7)
returns true if and only if the functions f and g have identical behavior on every element of lst.
# let f i = i * i;;
val f : int -> int = <fun>
# let g i = 3 * i;;
val g : int -> int = <fun>
# equiv_on f g [3];;
– : bool = true
# equiv_on f g [1;2;3];;
– : bool = false
8. Write a functions called pairwisefilter with two parameters: (i) a function cmp that compares two (7)
elements of a specific T and returns one of them, and (ii) a list lst of elements of that same type T. It
returns a list that applies cmp while taking two items at a time from lst. If lst has odd size, the last
element is returned “as is”.
# pairwisefilter min [14; 11; 20; 25; 10; 11];;
– : int list = [11; 20; 10]
# (* assuming that shorter : string * string -> string = <fun> already exists *)
# pairwisefilter shorter [“and”; “this”; “makes”; “shorter”; “strings”; “always”; “win”];;
– : string list = [“and”; “makes”; “always”; “win”]