X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=assignment5.mdwn;h=43c3ef55fe6ecc8ad477c32c0ab670268ff7cb6f;hp=ccf402abaa4a8b890b601146539978d6c2eca0fb;hb=e519121696a33c116b0942cb289e74d4d978b80c;hpb=96a8c8c9b81fc914ac7ec368fab0ffa4bcf4177a diff --git a/assignment5.mdwn b/assignment5.mdwn index ccf402ab..43c3ef55 100644 --- a/assignment5.mdwn +++ b/assignment5.mdwn @@ -1,4 +1,17 @@ +Assignment 5 + Types and OCAML +--------------- + +0. Recall that the S combinator is given by \x y z. x z (y z). + Give two different typings for this function in OCAML. + To get you started, here's one typing for K: + + # let k (y:'a) (n:'b) = y;; + val k : 'a -> 'b -> 'a = + # k 1 true;; + - : int = 1 + 1. Which of the following expressions is well-typed in OCAML? For those that are, give the type of the expression as a whole. @@ -108,3 +121,20 @@ or of `match`. That is, you must keep the `let` statements, though you're allowed to adjust what `b`, `y`, and `n` get assigned to. [[Hint assignment 5 problem 3]] + +4. Baby monads. Read the lecture notes for week 6, then write a + function `lift` that generalized the correspondence between + and + `add`: that is, `lift` takes any two-place operation on integers + and returns a version that takes arguments of type `int option` + instead, returning a result of `int option`. In other words, + `lift` will have type + + (int -> int -> int) -> (int option) -> (int option) -> (int option) + + so that `lift (+) (Some 3) (Some 4)` will evalute to `Some 7`. + Don't worry about why you need to put `+` inside of parentheses. + You should make use of `bind` in your definition of `lift`: + + let bind (x: int option) (f: int -> (int option)) = + match x with None -> None | Some n -> f n;; +