Here's my code
let rec Interest a b c =
if (c=0) then b else Interest(a ((1.0+a)*b) (c-1));;
The error is:
if (c=0) then b else Interest(a ((1.0+a)*b) (c-1));;
-------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stdin(2,26): error FS0001: Type mismatch. Expecting a 'a but given a 'a -> int -> 'a The resulting type would be infinite when unifying ''a' and ''a -> int -> 'a' >
You've defined Interest
as a function that takes three arguments but what you pass doesn't match that. The way it's written, you're only passing in a single argument to the recursive call to Interest - the parenthesized expression a ((1.0=a)*b) (c-1)
. Here, a
is inferred to be a function of two arguments, ((1.0=a)*b)
and (c-1)
.
Long story short, this throws off the type inference algorithm, until it gives up saying that it can't get a hang of what type to give to Interest
.
What you want is this:
let rec Interest a b c =
if (c=0) then b else Interest a ((1.0=a)*b) (c-1)
You'll also have a problem with (1.0=a)
. This evaluates to a boolean value that you later want to multiply with b
. Not sure what the intent is, but you might be looking for something like (if 1.0 = a then 1 else 0)
?
Unlike C-like languages that support implicit conversions between "bools" and ints, F# requires you to make all your conversions explicit to ensure correctness (this goes for converting between numeric types as well).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments