curried_flip etc
authorJim <jim.pryor@nyu.edu>
Sun, 8 Feb 2015 00:12:19 +0000 (19:12 -0500)
committerJim <jim.pryor@nyu.edu>
Sun, 8 Feb 2015 00:12:19 +0000 (19:12 -0500)
rosetta1.mdwn
topics/week1_kapulet_advanced.mdwn

index fa18958..32e4102 100644 (file)
@@ -157,6 +157,25 @@ Here the last displayed line will fail, because `add` expects as its argument a
 
 Kapulet essentially works like OCaml and Haskell; though for pedagogical reasons we started out by introducing uncurried definitions, rather than the *curried* definitions those other languages predominantly use.
 
 
 Kapulet essentially works like OCaml and Haskell; though for pedagogical reasons we started out by introducing uncurried definitions, rather than the *curried* definitions those other languages predominantly use.
 
+Here are some interesting functions we can define in Kapulet. See [[below|rosetta1#curried-patterns]] for the pattern syntax used here.
+
+    # Kapulet
+    let
+      curry   match lambda f. lambda  x  y.  f (x, y);
+      uncurry match lambda g. lambda (x, y). g  x  y ;
+      uncurried_flip match lambda f. lambda (y, x). f (x, y)
+      curried_flip   match lambda g. lambda  y  x.  g  x  y;
+    in ...
+
+The function `curry` takes as an argument a function `f` that expects its arguments *uncurried*, and returns instead a function `lambda x y. f (x, y)` a function that expects its arguments *curried* --- but then does with them whatever `f` does. Going in the other direction, the function `uncurry` takes a function `g` that expects its arguments *curried*, and returns instead a function that expects its arguments *uncurried* --- but then does with them whatever `g` does.
+
+The function `uncurried_flip` takes as an argument again an uncurried function `f`, and returns another function that also expects its arguments uncurried, but that expects them in the other order. `curried_flip` transforms a curried function `g` in the analogous way. These are both different from the function `swap` we defined in the [[course notes|topics/week1_kapulet_advanced#functions]] as:
+
+    lambda (x, y) = (y, x)
+
+*That* function operates on a tuple and returns another tuple. The `..._flip` functions operate on functions, and transform them into other functions that expect their arguments in a different order.
+
+
 <a id=sections></a>
 [[As we mentioned in the course notes|topics/week1_kapulet_advanced#sections]], in Kapulet, OCaml, and Haskell, there is a shorthand that enables you to write things like:
 
 <a id=sections></a>
 [[As we mentioned in the course notes|topics/week1_kapulet_advanced#sections]], in Kapulet, OCaml, and Haskell, there is a shorthand that enables you to write things like:
 
@@ -444,7 +463,7 @@ Kapulet's `(comp)`, `odd?`, `even?`, and `swap` are Haskell's `( . )`, `odd`, `e
 
 Kapulet's `dup` isn't predefined in Haskell but can be easily expressed as `\x -> (x, x)`.
 
 
 Kapulet's `dup` isn't predefined in Haskell but can be easily expressed as `\x -> (x, x)`.
 
-These are the same in Kapulet and Haskell (modulo the differences between [[Kapulet's multivalues|topics/week1_kapulet_intro#lightweight]] or "lightweight tuples" and Haskell's tuples): `id`, `const`, `flip`, `curry`, `uncurry`. None of these are predefined in OCaml.
+These are the same in Kapulet and Haskell (modulo the differences between [[Kapulet's multivalues|topics/week1_kapulet_intro#lightweight]] or "lightweight tuples" and Haskell's tuples): `id`, `const`, `curry`, `uncurry`. Kapulet's `curried_flip` is Haskell's `flip`.  None of these are predefined in OCaml.
 
 Kapulet and Haskell both have `( $ )`, which was explained [[in the course notes|topics/week1_kapulet_advanced#dollar]]. OCaml expresses this as `( @@ )`. (OCaml also uses `|>` to express the converse operation: `f x`, `f @@ x` and `x |> f` all mean the same.)
 
 
 Kapulet and Haskell both have `( $ )`, which was explained [[in the course notes|topics/week1_kapulet_advanced#dollar]]. OCaml expresses this as `( @@ )`. (OCaml also uses `|>` to express the converse operation: `f x`, `f @@ x` and `x |> f` all mean the same.)
 
@@ -605,6 +624,7 @@ This is similar to Scheme's `when` construction. Kapulet and Haskell have no ana
 
 ### Lambda expressions
 
 
 ### Lambda expressions
 
+<a id=curried-patterns></a>
 In Kapulet you write &lambda; expressions (sometimes called "anonymous functions") with a prefix of either &lambda; or the spelled-out `lambda`. That's followed by one or more patterns, separated by spaces, then a period, then a single expression which makes up the body of the function. When there are multiple patterns, the function expressed is *curried*, thus:
 
     lambda (x, y) z. result
 In Kapulet you write &lambda; expressions (sometimes called "anonymous functions") with a prefix of either &lambda; or the spelled-out `lambda`. That's followed by one or more patterns, separated by spaces, then a period, then a single expression which makes up the body of the function. When there are multiple patterns, the function expressed is *curried*, thus:
 
     lambda (x, y) z. result
index 2137d11..06a2043 100644 (file)
@@ -173,6 +173,8 @@ We've already come across the `id` function, namely &lambda; `x. x`.
 Other common functions are `fst`, which takes two arguments and returns the first of them; `snd`, which takes two arguments and returns the second of them; and `swap`, which takes two arguments and returns them both but with their positions swapped. A fourth function is `dup`, which takes one argument and returns it twice.
 These functions can be defined like this:
 
 Other common functions are `fst`, which takes two arguments and returns the first of them; `snd`, which takes two arguments and returns the second of them; and `swap`, which takes two arguments and returns them both but with their positions swapped. A fourth function is `dup`, which takes one argument and returns it twice.
 These functions can be defined like this:
 
+<a id=functions></a>
+
     let
       fst (x, y) = x;
       snd (x, y) = y;
     let
       fst (x, y) = x;
       snd (x, y) = y;