If you like Lisp syntax you can prefix operators using parentheses (heck...what else?):

1
let rec fac n = (iffun ((=) n 0) 1 ((*) n (fac ((-) n 1))))

The problem is that

1
iffun c t f

is DIFFERENT from

1
2
3
if c then t else f

because if you use an if statement, and the condition is true, the 'else' expression never gets evaluated, while it has to, in order to be passed to a function. So in your case you invoke (fac (-1)) to evaluate the last argument to the if, and there's no terminating condition for that.
Changing the test to <= 0 doean't fix the issue, because you still need to evaluate an infinite # of 'else' branches.

You can get out of it using lazy values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let iffun b f s = if b then f else s

let rec fac (n : Lazy<int>) =
    iffun
        (n.Value <= 0)
        (lazy(1))
        (lazy
            ((*)
                n.Value
                (fac
                    (lazy
                        (((-) n.Value 1))
                    )
                ).Value
            )
        )


printfn "%d" (fac (lazy(10))).Value

Oh my [:)]

By on 6/8/2010 5:34 AM ()

Thanks for the quick answer

I forgot that F# is not lazy by default

By on 6/8/2010 5:48 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper