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

type constructor question

Expand Messages
  • pinomugo149
    HI I m very newbie in programming, and I m trying to learn OCaml bases.. first of all SORRY for my bad english ;)) Well. I do not understand why in this
    Message 1 of 3 , Sep 11 3:22 AM
      HI
      I'm very newbie in programming, and I'm trying to learn OCaml bases..
      first of all SORRY for my bad english ;))
      Well. I do not understand why in this example of a guide that i'm
      reading ther're two "paired" args declared in e Constructor Node:

      # type 'a tree =
      Empty
      | Node of 'a * 'a tree list ;; (* here " 'a tree " type arg with
      list type arg without *--??? then I don't know as I have to call: Node
      (_, bs) *)
      # let rec height =
      let max_list l = List.fold_left max 0 l in
      function
      Empty -> 0
      | Node (_, bs) -> 1 + (max_list (List.map height bs)) ;;
      val height : 'a tree -> int = <fun>

      But if I call:
      # height [1; 2; 3]
      Error: This function is applied to too many arguments; ---(* ????*)..
      maybe you forgot a `;'

      I did not understand why, and where is the problem and why "too many
      args to Node...
      Thanks in advace for your help!!!


      [Non-text portions of this message have been removed]
    • Dan Bensen
      # type a tree = Empty | Node of a * a tree list ... Node is a value constructor (data constructor). The type constructor is tree. ... reading ther re two
      Message 2 of 3 , Sep 11 3:54 AM
        # type 'a tree = Empty | Node of 'a * 'a tree list

        > type constructor question

        Node is a value constructor (data constructor).
        The type constructor is tree.

        > I do not understand why in this example of a guide that i'm
        reading ther're two "paired" args declared in e Constructor Node:

        The first element in Node (x,nodes) is the label of the node.
        The second element is the subnodes.

        > I did not understand why, and where is the problem and why
        > "too many args to Node...

        It sounds like you called Node x nodes instead of Node (x,nodes).
        All OCaml data constructors with more than one argument take
        the arguments together in a tuple.

        I didn't get the "too many args" error. From the session
        you posted, I got a type error:

        # height [1; 2; 3];;
        Error: This expression has type 'a list
        but an expression was expected of type 'b tree

        But it works with a node:

        # height (Node (1,[Node (2,[Empty]);Empty]));;
        - : int = 2

        --Dan





        ________________________________
        From: pinomugo149 <pinomugo149@...>
        To: ocaml_beginners@yahoogroups.com
        Sent: Tue, September 11, 2012 5:22:32 AM
        Subject: "ocaml_beginners"::[] type constructor question


        HI
        I'm very newbie in programming, and I'm trying to learn OCaml bases..
        first of all SORRY for my bad english ;))
        Well. I do not understand why in this example of a guide that i'm
        reading ther're two "paired" args declared in e Constructor Node:

        # type 'a tree =
        Empty
        | Node of 'a * 'a tree list ;; (* here " 'a tree " type arg with
        list type arg without *--??? then I don't know as I have to call: Node
        (_, bs) *)
        # let rec height =
        let max_list l = List.fold_left max 0 l in
        function
        Empty -> 0
        | Node (_, bs) -> 1 + (max_list (List.map height bs)) ;;
        val height : 'a tree -> int = <fun>

        But if I call:
        # height [1; 2; 3]
        Error: This function is applied to too many arguments; ---(* ????*)..
        maybe you forgot a `;'

        I did not understand why, and where is the problem and why "too many
        args to Node...
        Thanks in advace for your help!!!

        [Non-text portions of this message have been removed]




        [Non-text portions of this message have been removed]
      • pinomugo149
        HI Dan and THANKS for your help! Yes, the problem was that!!I called data costructor without tuple.. now the story begins to become clearer ;) my doubt was
        Message 3 of 3 , Sep 11 7:03 AM
          HI Dan and THANKS for your help!
          Yes, the problem was that!!I called data costructor without tuple..
          now the story begins to become clearer ;)
          my doubt was about:

          type 'a tree =
          Empty
          | Node of 'a * 'a tree list ;;

          the type of second Node's argument : 'a tree list;
          then it sounds like a list that embeddeds a "tree value costructor"..Is that right?
          Thanks again!
        Your message has been successfully submitted and would be delivered to recipients shortly.