Re: Where is the syntax error

"Dario Teixeira [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
Hi,

>Now Im stuck at this problem : > 
>let rec power a b = if b = 1 then a * 1 else a * power ( b - 1) ;;
>
>Error: This expression has type int -> int
>       but an expression was expected of type int
The error message is telling you that the invocation of power is
missing an argument.  Also, I suggest using meaningful names for

your arguments, and note that multiplying by 1 is just silly.
Here's a better version:
  let rec power base exp = if exp = 1 then base else base * power base (exp - 1)


Note that the function above is neither tail-recursive nor resilient
against bad arguments (try giving it a negative exponent!).  Consider
improving it further as an exercise...

Best regards,
Dario Teixeira
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.