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

why do I get expression type error?

Expand Messages
  • superpyrin
    Hello, when I define following function (computes a**n where a is float and n is int) let rec ipow2 a n = if n = 0 then 1. else if n = 1 then a else if (n mod
    Message 1 of 2 , Nov 3, 2010
      Hello, when I define following function (computes a**n where a is float
      and n is int)

      let rec ipow2 a n =
      if n = 0 then 1. else if n = 1 then a
      else
      if (n mod 2) = 1 then
      let an = ipow2 a (n-1)/2 in
      a *. an *. an
      else
      let an = ipow2 a n/2 in
      an *. an

      I get following error:

      This expression has type float but is here used with type int

      However, when I define the function like this

      let rec ipow2 a n =
      if n = 0 then 1. else if n = 1 then a
      else
      if (n mod 2) = 1 then
      let n2 = (n-1)/2 in
      let an = ipow2 a n2 in
      a *. an *. an
      else
      let n2 = n/2 in
      let an = (ipow2 a n2) in
      an *. an

      it is accepted. I don't understand what is wrong in the first
      definition. Can somebody explain? I am using "The Objective Caml
      toplevel, version 3.10.2". Thank you.



      [Non-text portions of this message have been removed]
    • Mathias Kende
      ... Your application of the ipow2 function is incorrect. In : let an = ipow2 a (n-1)/2 in the argument to ipow2 are a abd (n-1) (well typed), which is
      Message 2 of 2 , Nov 3, 2010
        Le mercredi 03 novembre 2010 à 20:36 +0000, superpyrin a écrit :

        > This expression has type float but is here used with type int

        Your application of the ipow2 function is incorrect. In :
        let an = ipow2 a (n-1)/2 in
        the argument to ipow2 are a abd (n-1) (well typed), which is sufficient
        for a complete application of the function. The result of the
        application (a float) is then given to the infix operator ( / ) with
        also the argument 2. The operator expects an int, hence the error.

        You want to group the (n-1)/2 in a parenthesis to avoid this.

        As a side note, the error message should explain this to you, because
        the character range it gives is probably from ipow2 to (n-1), meaning
        that this is one expression for the compiler and that this expression
        has type float (it is not an un applied function and the error message
        does not refer to just one of the arguments of the function).

        Mathias

        >
        > However, when I define the function like this
        >
        > let rec ipow2 a n =
        > if n = 0 then 1. else if n = 1 then a
        > else
        > if (n mod 2) = 1 then
        > let n2 = (n-1)/2 in
        > let an = ipow2 a n2 in
        > a *. an *. an
        > else
        > let n2 = n/2 in
        > let an = (ipow2 a n2) in
        > an *. an
        >
        > it is accepted. I don't understand what is wrong in the first
        > definition. Can somebody explain? I am using "The Objective Caml
        > toplevel, version 3.10.2". Thank you.
        >
        > [Non-text portions of this message have been removed]
        >
        >
        >
        >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.