From c502c251f0d7de9c13847efb96b9d5807b8defd9 Mon Sep 17 00:00:00 2001 From: Jim Pryor Date: Sat, 4 Dec 2010 09:31:48 -0500 Subject: [PATCH] translating tweaks Signed-off-by: Jim Pryor --- translating_between_OCaml_Scheme_and_Haskell.mdwn | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/translating_between_OCaml_Scheme_and_Haskell.mdwn b/translating_between_OCaml_Scheme_and_Haskell.mdwn index e8b897f1..2300069a 100644 --- a/translating_between_OCaml_Scheme_and_Haskell.mdwn +++ b/translating_between_OCaml_Scheme_and_Haskell.mdwn @@ -575,7 +575,7 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a 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 ``. 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 ``. 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. -- 2.11.0