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

set_map

Expand Messages
  • dvanhorn_at_uvm
    Could someone tell what is wrong with this code. I would expect the result to be [2]. Thanks -- David module IntSet = Set.Make(struct type t = int let
    Message 1 of 3 , Jul 1, 2004
    • 0 Attachment
      Could someone tell what is wrong with this code. I would expect the
      result to be [2]. Thanks -- David

      module IntSet =
      Set.Make(struct type t = int let compare = compare end)
      open IntSet
      let set_map f = fold (fun x s -> (add (f x) s)) empty

      elements (set_map ((+) 1) (singleton 1))
      - : IntSet.elt list = [1]
    • dvanhorn_at_uvm
      ... Dumb question. Sorry for the noise. I had the order of parameters to fold switched up. David
      Message 2 of 3 , Jul 1, 2004
      • 0 Attachment
        --- In ocaml_beginners@yahoogroups.com, "dvanhorn_at_uvm" <david@v...>
        wrote:
        > Could someone tell what is wrong with this code. I would expect the
        > result to be [2]. Thanks -- David
        >
        > module IntSet =
        > Set.Make(struct type t = int let compare = compare end)
        > open IntSet
        > let set_map f = fold (fun x s -> (add (f x) s)) empty
        >
        > elements (set_map ((+) 1) (singleton 1))
        > - : IntSet.elt list = [1]

        Dumb question. Sorry for the noise. I had the order of parameters to
        fold switched up.

        David
      • Yamagata Yoriyuki
        From: dvanhorn_at_uvm Subject: ocaml_beginners ::[] set_map Date: Thu, 01 Jul 2004 17:13:22 -0000 ... The order of the arguments of fold
        Message 3 of 3 , Jul 1, 2004
        • 0 Attachment
          From: "dvanhorn_at_uvm" <david@...>
          Subject: "ocaml_beginners"::[] set_map
          Date: Thu, 01 Jul 2004 17:13:22 -0000

          > Could someone tell what is wrong with this code. I would expect the
          > result to be [2]. Thanks -- David
          >
          > module IntSet =
          > Set.Make(struct type t = int let compare = compare end)
          > open IntSet
          > let set_map f = fold (fun x s -> (add (f x) s)) empty
          >
          > elements (set_map ((+) 1) (singleton 1))
          > - : IntSet.elt list = [1]

          The order of the arguments of fold is wrong. You should use

          let set_map f s0 = fold (fun x s -> add (f x) s) s0 empty
          ^^
          The set applied to the iterator comes to the second position. In
          general, I would advise you to avoid overuse of Currying.

          --
          Yamagata Yoriyuki
        Your message has been successfully submitted and would be delivered to recipients shortly.