translating tweaks
[lambda.git] / translating_between_OCaml_Scheme_and_Haskell.mdwn
index 171f644..77db443 100644 (file)
@@ -289,7 +289,7 @@ So we have:
 
        In OCaml, there is no predefined `null` or `isempty` function. One can still test whether a list is empty using the comparison `lst = []`.
 
-*      In Haskell, the expression [1..5] is the same as [1,2,3,4,5], and the expression [0..] is a infinite lazily-evaluated stream of the natural numbers. In OCaml, there is no [1..5] shortcut, lists must be finite, and they are eagerly evaluated. It is possible to create lazy streams in OCaml, even infinite ones, but you have to use other techniques than the native list type.
+*      In Haskell, the expression `[1..5]` is the same as `[1,2,3,4,5]`, and the expression `[0..]` is a infinite lazily-evaluated stream of the natural numbers. In OCaml, there is no `[1..5]` shortcut, lists must be finite, and they are eagerly evaluated. It is possible to create lazy streams in OCaml, even infinite ones, but you have to use other techniques than the native list type.
 
 *      Haskell has *list comprehensions*:
 
@@ -299,7 +299,7 @@ So we have:
 
                List.map (fun x -> x * x) (List.filter odd [1..10]);;
 
-*      In Haskell, the expressions "abc" and ['a','b','c'] are equivalent. (Strings are just lists of chars.) In OCaml, these expressions have two different types.
+*      In Haskell, the expressions `"abc"` and `['a','b','c']` are equivalent. (Strings are just lists of chars.) In OCaml, these expressions have two different types.
 
        Haskell uses the operator `++` for appending both strings and lists (since Haskell strings are just one kind of list). OCaml uses different operators:
 
@@ -508,15 +508,15 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
 
        you can also write either of:
 
-               (1 >) 2
-               (> 2) 1
+               (2 >) 1
+               (> 1) 2
 
        In OCaml one has to write these out longhand:
 
-               (fun y -> 1 > y) 2;;
-               (fun x -> x > 2) 1;;
+               (fun y -> 2 > y) 1;;
+               (fun x -> x > 1) 2;;
 
-       Also, in Haskell, there's a special syntax for using what are ordinarily prefix functions into infix operators:
+       Also, in Haskell, there's a special syntax for using what are ordinarily prefix functions as infix operators:
 
                Prelude> elem 1 [1, 2]
                True
@@ -568,14 +568,14 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
 
 *      Some functions are predefined in Haskell but not in OCaml. Here are OCaml definitions for some common ones:
 
-       let id x = x;;
-       let const x _ = x;;
-       let flip f x y = f y x;;
-       let curry (f : ('a, 'b) -> 'c) = fun x y -> f (x, y);;
-       let uncurry (f : 'a -> 'b -> 'c) = fun (x, y) -> f x y;;
-       let null lst = lst = [];;
+               let id x = x;;
+               let const x _ = x;;
+               let flip f x y = f y x;;
+               let curry (f : ('a, 'b) -> 'c) = fun x y -> f (x, y);;
+               let uncurry (f : 'a -> 'b -> 'c) = fun (x, y) -> f x y;;
+               let null lst = lst = [];;
 
-       `fst` and `snd` (defined only on pairs) are provided in both languages. Haskell has `head` and `tail` for lists; these will raise an exception if applied to []. In OCaml the corresponding functions are `List.hd` and `List.tl`. Many other Haskell list functions like `length` are available in OCaml as `List.length`, but OCaml's standard libraries are leaner that Haskell's.
+       `fst` and `snd` (defined only on pairs) are provided in both languages. Haskell has `head` and `tail` for lists; these will raise an exception if applied to `[]`. In OCaml the corresponding functions are `List.hd` and `List.tl`. Many other Haskell list functions like `length` are available in OCaml as `List.length`, but OCaml's standard libraries are leaner that Haskell's.
 
 *      The `until` function in Haskell is used like this:
 
@@ -587,8 +587,8 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
 
        This can be defined in OCaml as:
 
-    let rec until test f z =
-        if test z then z else until test f (f z)
+               let rec until test f z =
+                 if test z then z else until test f (f z)
 
 
 #Lazy or Eager#
@@ -615,14 +615,14 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
                # eval_later3;;
                - : int lazy_t = lazy 1
 
-       Notice in the last line the value is reported as being `lazy 1` instead of `<lazy>`. Since the value has once been forced, it won't ever need to be recomputed. The thunks are less efficient in this respect. Even though OCaml will now remember that `eval_later3` should be forced to, `eval_later3` is still type distinct from a plain `int`.
+       Notice in the last line the value is reported as being `lazy 1` instead of `<lazy>`. Since the value has once been forced, it won't ever need to be recomputed. The thunks are less efficient in this respect. Even though OCaml will now remember what `eval_later3` should be forced to, `eval_later3` is still type-distinct from a plain `int`.
 
 
 #Monads#
 
 Haskell has more built-in support for monads, but one can define the monads one needs in OCaml.
 
-*      In our seminar, we've been calling one monadic operation `unit`, in Haskell the same operation is called `return`. We've been calling another monadic operation `bind`, used in prefix form, like this:
+*      In our seminar, we've been calling one monadic operation `unit`; in Haskell the same operation is called `return`. We've been calling another monadic operation `bind`, used in prefix form, like this:
 
                bind u f
 
@@ -630,13 +630,13 @@ Haskell has more built-in support for monads, but one can define the monads one
 
                u >>= f
 
-       If you like this Haskell convention, you can define (>>=) in OCaml like this:
+       If you like this Haskell convention, you can define `>>=` in OCaml like this:
 
                let (>>=) = bind;;
 
 *      Haskell also uses the operator `>>`, where `u >> v` means the same as `u >>= \_ -> v`.
 
-*      In Haskell, one can generally just use plain `return` and `>>=` and the compiler will infer what monad you must be talking about from the surrounding type constraints. In OCaml, you generally need to be specific about which monad you're using. So in these notes, when mutiple monads are on the table, we've defined operations as `reader_unit` and `reader_bind`.
+*      In Haskell, one can generally just use plain `return` and `>>=` and the interpreter will infer what monad you must be talking about from the surrounding type constraints. In OCaml, you generally need to be specific about which monad you're using. So in these notes, when mutiple monads are on the table, we've defined operations as `reader_unit` and `reader_bind`, and so on.
 
 *      Haskell has a special syntax for working conveniently with monads. It looks like this. Assume `u` `v` and `w` are values of some monadic type `M a`. Then `x` `y` and `z` will be variables of type `a`:
 
@@ -653,7 +653,7 @@ Haskell has more built-in support for monads, but one can define the monads one
                v >>= \ y ->
                w >>= \ _ ->
                let z = foo x y
-               in unit z
+               in return z
 
        which can be translated straightforwardly into OCaml.