X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=translating_between_OCaml_Scheme_and_Haskell.mdwn;h=338a2966824788ddbac301e710cba7039eb27095;hp=2e33b1a2b94b0fff2794a3bc8a0380ef26bf52fd;hb=45bb412ca548c5cd02967d2b6c4d7ce2b0a88f0b;hpb=323b2661061ae22ca28b4bf79564e1d1bc9bc446 diff --git a/translating_between_OCaml_Scheme_and_Haskell.mdwn b/translating_between_OCaml_Scheme_and_Haskell.mdwn index 2e33b1a2..338a2966 100644 --- a/translating_between_OCaml_Scheme_and_Haskell.mdwn +++ b/translating_between_OCaml_Scheme_and_Haskell.mdwn @@ -385,7 +385,9 @@ We will however try to give some general advice about how to translate between O ##Records## -Haskell and OCaml both have `records`, which are essentially just tuples with a pretty interface. The syntax for declaring and using these is a little bit different in the two languages. +Haskell and OCaml both have `records`, which are essentially just tuples with a pretty interface. We introduced these in the wiki notes [here](/coroutines_and_aborts/). + +The syntax for declaring and using these is a little bit different in the two languages. * In Haskell one says: @@ -432,7 +434,7 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a In OCaml: - let { red = r; green = g } = c + let { red = r; green = g; _ } = c in r In Haskell: @@ -446,11 +448,37 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a In OCaml it's: - # let makegray ({red = r} as c) = { c with green=r; blue=r };; + # let makegray ({ red = r; _ } as c) = { c with green=r; blue=r };; val makegray : color -> color = # makegray { red = 0; green = 127; blue = 255 };; - : color = {red = 0; green = 0; blue = 0} +* Records just give your types a pretty interface; they're entirely dispensable. Instead of: + + type color = { red : int; green : int; blue : int };; + let c = { red = 0; green = 127; blue = 255 };; + let r = c.red;; + + You could instead just use a more familiar data constructor: + + type color = Color of (int * int * int);; + let c = Color (0, 127, 255);; + + and then extract the field you want using pattern-matching: + + let Color (r, _, _) = c;; + (* or *) + match c with Color (r, _, _) -> ... + + (Or you could just use bare tuples, without the `Color` data constructor.) + + The record syntax only exists because programmers sometimes find it more convenient to say: + + ... c.red ... + + than to reach for those pattern-matching constructions. + + ##Functions##