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

4899Re: "ocaml_beginners"::[] unbound value

Expand Messages
  • Richard Jones
    Nov 1, 2005
    • 0 Attachment
      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
    • Show all 15 messages in this topic