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

13941which syntax in function composition is better

Expand Messages
  • Jean Saint-Remy
    Aug 10, 2013
      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]
    • Show all 2 messages in this topic