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

unbound value

Expand Messages
  • Java
    Hi, I m writing a couple of modules implementing a binary tree. Im proceeding adding a functions one by one becouse of checking with ocaml interpreter. Here
    Message 1 of 15 , Nov 1, 2005
      Hi,

      I'm writing a couple of modules implementing a binary tree. Im
      proceeding adding a functions one by one becouse of checking with ocaml
      interpreter. Here the code:

      [start code]

      ---interface--
      module type BALBERO =
      sig
      type 'a tree

      exception EmptyEx

      val emptytree: 'a tree

      val insert: 'a * 'a tree -> 'a tree

      val search: 'a * 'a tree -> bool
      end;;

      ---Semantic implementation---
      [...] (*insert function works fine*)

      let rec search (x, tree) =
      match tree with
      | Empty -> false
      | Node (y, left, rigth) ->
      if (x = y) then true else
      if (x > y) then search(x, right)
      else search(x, left)

      end;;

      [end code]

      For the function "search" the interpreter returns me this error:
      Characters 475-480:
      Unbound value right

      Why?? The insert function is very similar to search and works fine.

      Thanks by now
      Segolas
    • micha
      On Tue, 01 Nov 2005 12:59:21 +0100 ... that s rigth not right, so ocaml is right :-) Michael
      Message 2 of 15 , Nov 1, 2005
        On Tue, 01 Nov 2005 12:59:21 +0100
        Java <quilospam@...> wrote:

        > Hi,
        >
        > I'm writing a couple of modules implementing a binary tree. Im
        > proceeding adding a functions one by one becouse of checking with
        > ocaml interpreter. Here the code:
        > ...
        > let rec search (x, tree) =
        > match tree with
        > | Empty -> false
        > | Node (y, left, rigth) ->
        ----------------------------------^^^
        that's rigth not right, so ocaml is right :-)

        Michael
      • Java
        ... ARGH! I lost lots of time checking errors like theese, anyone knows if is there a possibility to let ocaml interpreter highlight the error line? It only
        Message 3 of 15 , Nov 1, 2005
          > that's rigth not right, so ocaml is right :-)
          >
          ARGH! I lost lots of time checking errors like theese, anyone knows if
          is there a possibility to let ocaml interpreter highlight the error
          line? It only says "unbound value " or "syntax error at characters
          345-347".

          regards
          Segolas
        • Jon Harrop
          ... It should underline the error by default. Are you using Windows or something? -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for
          Message 4 of 15 , Nov 1, 2005
            On Tuesday 01 November 2005 13:50, Java wrote:
            > > that's rigth not right, so ocaml is right :-)
            >
            > ARGH! I lost lots of time checking errors like theese, anyone knows if
            > is there a possibility to let ocaml interpreter highlight the error
            > line? It only says "unbound value " or "syntax error at characters
            > 345-347".

            It should underline the error by default. Are you using Windows or something?

            --
            Dr Jon D Harrop, Flying Frog Consultancy Ltd.
            Objective CAML for Scientists
            http://www.ffconsultancy.com/products/ocaml_for_scientists
          • Richard Jones
            On Tue, Nov 01, 2005 at 12:59:21PM +0100, Java wrote: [...] Someone else has found the obvious error; I have a few other ... You re using tuples as parameters,
            Message 5 of 15 , Nov 1, 2005
              On Tue, Nov 01, 2005 at 12:59:21PM +0100, Java wrote:
              [...]

              Someone else has found the obvious error; I have a few other
              observations to make:

              > val insert: 'a * 'a tree -> 'a tree
              >
              > val search: 'a * 'a tree -> bool

              You're using tuples as parameters, like ML (IIRC). Your code will run
              significantly faster if you define functions with separate parameters,
              like this:

              (* interface: *)
              val search : 'a -> 'a tree -> bool

              (* implementation: *)
              let rec search x tree =
              match tree with
              | Empty -> false
              | Node (y, left, right) ->
              if (x = y) then true
              else if (x > y) then search x right
              else search x left

              Now, the above code can be made shorter (but probably not any faster)
              by writing instead:

              let rec search x = function
              | Empty -> false
              | Node (y, left, right) ->
              if (x = y) then true
              else if (x > y) then search x right
              else search x left

              and as a stylistic alternative you could use guards, although again
              this will not necessarily improve the speed of the code (might even
              make it slower):

              let rec search x = function
              | Empty -> false
              | Node (y, _, _) when x = y -> true
              | Node (y, _, right) when x > y -> search x right
              | Node (_, _, left) -> search x left

              > For the function "search" the interpreter returns me this error:
              > Characters 475-480:
              > Unbound value right

              You're using a rather ancient version of Ocaml. Since around 3.08,
              Ocaml has reported line number and position within line, which is a
              lot more useful for locating the source of errors.

              Rich.

              --
              Richard Jones, CTO Merjis Ltd.
              Merjis - web marketing and technology - http://merjis.com
              Team Notepad - intranets and extranets for business - http://team-notepad.com
            • William D. Neumann
              ... Well, it does. (********************************************************) Objective Caml version 3.09.0 # type a tree = ... let rec search (x, tree) =
              Message 6 of 15 , Nov 1, 2005
                On Tue, 1 Nov 2005, Java wrote:

                >> that's rigth not right, so ocaml is right :-)
                >>
                > ARGH! I lost lots of time checking errors like theese, anyone knows if
                > is there a possibility to let ocaml interpreter highlight the error
                > line? It only says "unbound value " or "syntax error at characters
                > 345-347".

                Well, it does.

                (********************************************************)
                Objective Caml version 3.09.0

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

                let rec search (x, tree) =
                match tree with
                | Empty -> false
                | Node (y, left, rigth) ->
                if (x = y) then true else
                if (x > y) then search(x, right)
                else search(x, left)
                ;;
                type 'a tree = Empty | Node of 'a * 'a tree * 'a tree
                # Characters 204-209:
                if (x > y) then search(x, right)
                ^^^^^
                Unbound value right
                (********************************************************)

                The problem here (well, not really a problem) is that this doesn't tell
                you what you wanted to know, which is that you really misspelled right in
                the match case. I suppose some logic could be built into the compiler to
                look for possible typos and suggest corrections, but I doubt it would be
                worth the effort to implement...

                William D. Neumann

                ---

                "There's just so many extra children, we could just feed the
                children to these tigers. We don't need them, we're not doing
                anything with them.

                Tigers are noble and sleek; children are loud and messy."

                -- Neko Case

                Life is unfair. Kill yourself or get over it.
                -- Black Box Recorder
              • Java
                I m getting crazy {O.o} first of all a very beginner question: writing let rec search x tree is the same as let rec search (x,tree) ???? (if not pleas
                Message 7 of 15 , Nov 1, 2005
                  I'm getting crazy {O.o}

                  first of all a very beginner question:
                  writing "let rec search x tree" is the same as "let rec search
                  (x,tree)"???? (if not pleas someone explain me the difference

                  and then, I write a code like this:
                  --interface--
                  val insert: 'a * 'a tree -> 'a tree

                  --implementation
                  let rec insert x tree = function
                  | Empty -> Node(x, Empty, Empty)
                  | Node (y, left, right) ->
                  if (x >= y) then Node(y, left, insert x right)
                  else Node(y, insert x left, right)

                  and the compiler says to me:
                  "This expression has type 'a tree -> 'a tree but is here used with type
                  'a tree"

                  why??

                  thanks
                  Segolas

                  p.s.
                  and tomorrow I'm going to take the exam of theese things...
                • Jonathan Bryant
                  ... No, it is not. (x, tree) is a tuple, which is a single type/entity/whatever word you want to use. parenthesis and commas are not used in OCaml to
                  Message 8 of 15 , Nov 1, 2005
                    On Tue, 2005-11-01 at 10:16, Java wrote:
                    > I'm getting crazy {O.o}
                    >
                    > first of all a very beginner question:
                    > writing "let rec search x tree" is the same as "let rec search
                    > (x,tree)"???? (if not pleas someone explain me the difference

                    No, it is not. (x, tree) is a tuple, which is a single
                    type/entity/whatever word you want to use. parenthesis and commas are
                    not used in OCaml to separate parameters like in other languages.

                    let rec search x tree

                    yields the type

                    val search : 'a -> 'a tree -> 'a tree.

                    let rec search (x, tree)

                    yields the type

                    let rec search ('a, 'a tree) -> 'a tree

                    One (the first one) takes two parameters: an 'a and an 'a tree. The
                    other (the second one) takes one parameter: a tuple of an 'a and an 'a
                    tree. Subtle, but important, difference.

                    >
                    > and then, I write a code like this:
                    > --interface--
                    > val insert: 'a * 'a tree -> 'a tree
                    >
                    > --implementation
                    > let rec insert x tree = function
                    > | Empty -> Node(x, Empty, Empty)
                    > | Node (y, left, right) ->
                    > if (x >= y) then Node(y, left, insert x right)
                    > else Node(y, insert x left, right)
                    >
                    > and the compiler says to me:
                    > "This expression has type 'a tree -> 'a tree but is here used with
                    > type
                    > 'a tree"
                    >
                    > why??
                    >
                    > thanks
                    > Segolas
                    >
                    > p.s.
                    > and tomorrow I'm going to take the exam of theese things...
                    >
                    >
                    >
                    >
                    > Archives up to August 22, 2005 are also downloadable at
                    > http://www.connettivo.net/cntprojects/ocaml_beginners/
                    > The archives of the very official ocaml list (the seniors' one) can be
                    > found at http://caml.inria.fr
                    > Attachments are banned and you're asked to be polite, avoid flames
                    > etc.
                    >
                    >
                    >
                    > ______________________________________________________________________
                    > YAHOO! GROUPS LINKS
                    > 1. Visit your group "ocaml_beginners" on the web.
                    >
                    > 2. To unsubscribe from this group, send an email to:
                    > ocaml_beginners-unsubscribe@yahoogroups.com
                    >
                    > 3. Your use of Yahoo! Groups is subject to the Yahoo! Terms of
                    > Service.
                    >
                    >
                    > ______________________________________________________________________
                    --
                    --Jonathan Bryant
                    jtbryant@...
                    Student Intern
                    Unix System Operations
                    VSU Information Technology

                    "Das Leben ohne Music ist einfach ein Irrtum, eine Strapaze, ein" Exil."
                    ("Life without music is simply an error, a pain, an exile.")
                    --Frederich Nietzsche

                    "The three cardinal values of a programmer are laziness, impatience, and
                    hubris."
                    --Perl Man Page
                  • Java
                    ... Ok, but I need to use tuples becouse I m defining data types for a didattical language. Or probably my teacher knows ML syntax and say us touse Ocaml
                    Message 9 of 15 , Nov 1, 2005
                      Richard Jones wrote:

                      >
                      > You're using tuples as parameters, like ML (IIRC). Your code will run
                      > significantly faster if you define functions with separate parameters,
                      > like this:
                      >
                      Ok, but I need to use tuples becouse I'm defining data types for a
                      didattical language. Or probably my teacher knows ML syntax and say us
                      touse Ocaml counting on some sort o retro-compatibility.

                      thanx
                    • Java
                      ... Ok, thanks for the link
                      Message 10 of 15 , Nov 1, 2005
                        > so writing:
                        >
                        > let rec insert x = function (* ... *)
                        >
                        > means make 'insert x' be a function taking one (additional) parameter.
                        >
                        > http://www.ocaml-tutorial.org/functional_programming#What_is_functional_programming_
                        > (and scroll down to "Partial function applications and currying").
                        >
                        > Rich.

                        Ok, thanks for the link
                      • Richard Jones
                        ... No they re not the same. let rec search x tree = (* definition *) defines a recursive function called search which takes two parameters. Whereas: let rec
                        Message 11 of 15 , Nov 1, 2005
                          On Tue, Nov 01, 2005 at 04:16:38PM +0100, Java wrote:
                          > first of all a very beginner question:
                          > writing "let rec search x tree" is the same as "let rec search
                          > (x,tree)"???? (if not pleas someone explain me the difference

                          No they're not the same.

                          let rec search x tree = (* definition *)

                          defines a recursive function called search which takes two parameters.
                          Whereas:

                          let rec search (x, tree) = (* definition *)

                          defines a recursive function called search which takes ONE parameter.
                          This parameter happens to be a pair. They have different type
                          signatures too, as you would expect.

                          > let rec insert x tree = function
                          > | Empty -> Node(x, Empty, Empty)
                          > | Node (y, left, right) ->
                          > if (x >= y) then Node(y, left, insert x right)
                          > else Node(y, insert x left, right)

                          This isn't what I wrote in the OP which is why it doesn't work ...

                          let rec insert x tree =
                          match tree with
                          | (* ... *)

                          is the same as:

                          let rec insert x = function
                          | (* ... *)

                          Pay close attention to the number of parameters declared after the
                          function name. Both functions will have the same signature (both will
                          take two parameters); for example:

                          # let rec insert x tree =
                          match tree with
                          | _ -> 1 ;;
                          val insert : 'a -> 'b -> int = <fun> <--- (1)
                          # let rec insert x = function
                          | _ -> 1 ;;
                          val insert : 'a -> 'b -> int = <fun> <--- (2)

                          (1) = (2)

                          The reason for this is that 'function' declares a function taking
                          a single parameter; so writing:

                          let rec insert x = function (* ... *)

                          means make 'insert x' be a function taking one (additional) parameter.

                          http://www.ocaml-tutorial.org/functional_programming#What_is_functional_programming_
                          (and scroll down to "Partial function applications and currying").

                          Rich.

                          --
                          Richard Jones, CTO Merjis Ltd.
                          Merjis - web marketing and technology - http://merjis.com
                          Team Notepad - intranets and extranets for business - http://team-notepad.com
                        Your message has been successfully submitted and would be delivered to recipients shortly.