Loading ...
Sorry, an error occurred while loading the content.

8743Re: "ocaml_beginners"::[] Choosing the order of parameters

Expand Messages
  • Fabrice Marchant
    Oct 1, 2007
    • 0 Attachment
      Thanks for these useful explanations and sorry for the delay !

      Martin :
      > Anyway, choose the order of arguments that is the most standard, if
      > applicable, or that makes partial application easier. But you'll always
      > have to write things like (fun x -> f x y) instead of (f y). This is not
      > too bad.

      Jon :
      > # module M = Map.Make(String);;
      >...
      > # let size m = M.fold (fun _ _ n -> n + 1) m 0;;

      instead of :
      let size = flip (M.fold (fun _ _ n -> n + 1)) 0

      Here the extra call to 'flip', that avoids to express a parameter, will
      probably slow down things a bit.

      -------------------------------------------------------------------

      Martin :
      > Note that labelled arguments, optional or not, are very useful when you
      > have a lot of parameters, especially if several of them have the same
      > type.

      Peng :
      > In fact, I use it all the time as many of my functions use labelled arguments
      > (it's convenient as then the type sig for the function yields more semantic
      > meaning).

      A bit like C programmers sometimes name parameters in function declarations
      inside their .h files to make things easier to grasp ?

      ------------------------------------------------------------------------------

      >> However what general recommendation should we follow, at design time, to
      >> choose the parameter order ? Maybe trying to imagine what could be the most
      >> "natural" way to use the function. But what does it exactly mean ?

      Jon :
      > If you're writing standard functions like "map" and "fold" then you
      > should adhere to the conventions. If you're writing your own custom
      > functions then try to rearrange the order of the arguments to maximize
      > the usefulness of currying, i.e. put the arguments that are most likely
      > to be partially applied first.
      Thanks a lot Jon !

      ------------------------------------------------------------------------
      Regards,

      Fabrice
    • Show all 18 messages in this topic