Factorial1 2 3 4 5 6 7 8 | import Data.Function
factF f = \n -> if n == 0 then 1 else n * f(n- 1 )
fact 0 = factF undefined
fact 1 = factF . factF $ undefined
fact 2 = factF . factF . factF $ undefined
fact 3 = factF . factF . factF . factF $ undefined
fact = fix factF
|
Run1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $ghci
Prelude>:load fact.hs
*Main> fact0 0
1
*Main> fact0 1
*** Exception: Prelude.undefined
*Main> fact1 0
1
*Main> fact1 1
1
*Main> fact1 2
*** Exception: Prelude.undefined
*Main> fact 100
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|