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

Re: "ocaml_beginners"::[] which syntax in function composition is better

Expand Messages
  • Lukasz Stafiniak
    In the first example, more is defined as a reference cell. In the second example, more is defined as a synonym of the unit value () . If you change the
    Message 1 of 2 , Aug 11, 2013
    • 0 Attachment
      In the first example, "more" is defined as a reference cell. In the second
      example, "more" is defined as a synonym of the unit value "()". If you
      change the second example as follows, they will be equivalent. But all
      three forms are bad style. In particular, "!more" should be replaced by
      "[]".

      let add tbl key data =
      let more =
      if (Hashtbl.mem tbl key) then raise (Invalid_argument "The item already
      exists in the hash table")
      else
      let more = ref [] in
      more := data :: !more; more in
      Hashtbl.add tbl key more ;;


      On Sun, Aug 11, 2013 at 7:18 AM, Jean Saint-Remy <jeansaintremy@...>wrote:

      > **
      >
      >
      > Hi,
      >
      > I came across an "awkward" function composition example and I wanted to
      > get a grasp when one syntax should be
      >
      > preferred over the other. I understand the straightforwardness and clarity
      > of the second form, but under what
      > circumstances would you use the first? I thought that defining an
      > accumulator might be one such example.
      >
      > "awkward example"
      > let add tbl key data =
      >
      > let more =
      >
      > if (Hashtbl.mem tbl key) then raise (Invalid_argument "The item
      > already exists in the hash table")
      > else
      > let more = ref [] in
      >
      > Hashtbl.add tbl key more;
      > more in
      > more := data :: !more ;;
      >
      > "straightforward example"
      > let add tbl key data =
      >
      > let more =
      >
      > if (Hashtbl.mem tbl key) then raise (Invalid_argument "The item already
      > exists in the hash table")
      > else
      > let more = ref [] in
      >
      > more := data :: !more in
      >
      > Hashtbl.add tbl key more ;;
      >
      > When I read the second version, I can "follow along" the function
      > composition from one statement to the next and understand
      > the program flow better. The first version I see "more" being redefined
      > after a semicolon. When I miss putting the semicolon,
      >
      > the program does not work and it seems like "more" is being redefined
      > after the fact. Both versions work and I am not at the
      > moment concerned with "performance penalty" or if there even is one.
      >
      > With kind regards,
      >
      > Jean
      >
      > [Non-text portions of this message have been removed]
      >
      >
      >


      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.