RE: Where is the syntax error
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
Thanks, I will do that later. Things like tail recursion is not discussed in the first three chapters. Roelof To: [email protected] From: [email protected] Date: Fri, 27 Feb 2015 12:19:26 +0000 Subject: Re: "ocaml_beginners"::[] Where is the syntax error 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