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

Fw: fun v function

Expand Messages
  • Steven Shaw
    ... From: Steven Shaw To: Sent: Thursday, July 04, 2002 10:17 PM Subject: fun v function ...
    Message 1 of 2 , Jul 4, 2002
    • 0 Attachment
      ----- Original Message -----
      From: "Steven Shaw" <steven_shaw@...>
      To: <ocaml_beginners@yahoogroups.com>
      Sent: Thursday, July 04, 2002 10:17 PM
      Subject: fun v function


      > The ocaml book advises against use of 'fun' and seems to be promoting the
      > use of 'function'. Is there any particular reason for this? The 'fun' way
      is
      > shorter and doesn't seem that bad.
      >
    • Martin Jambon
      ... function can be used for pattern matching one one argument, and can be used as a shortcut for match ... with : let print_bool b = match b with true -
      Message 2 of 2 , Jul 4, 2002
      • 0 Attachment
        On Thu, 4 Jul 2002, Steven Shaw wrote:

        > > The ocaml book advises against use of 'fun' and seems to be promoting the
        > > use of 'function'. Is there any particular reason for this? The 'fun' way
        > is
        > > shorter and doesn't seem that bad.

        function can be used for pattern matching one one argument, and can be
        used as a shortcut for match ... with :

        let print_bool b =
        match b with
        true -> print_string "true"
        | false -> print_string "false"

        Can be written:

        let print_bool =
        function
        true -> print_string "true"
        | false -> print_string "false"


        fun is used to write anonymous functions with possibly more than one
        argument (without pattern matching):

        List.map2 (fun x y -> x + 2 * y) [1;2;3] [4;2;6]


        Martin
      Your message has been successfully submitted and would be delivered to recipients shortly.