作者: Bruce.
日時: 2009/5/06(23:57)
Haskell別解。

>ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude> foldl (\x y -> x * y) 1 $ take 10 [1..]
3628800
Prelude> foldl (*) 1 $ take 10 [1..]
3628800
Prelude>

関数に仕立てるとこうかな

ffact :: Int -> Int
ffact n
    | n == 0  = 1
    | n > 0   = foldl (*) 1 $ take n [1..]
    | otherwise = error "invalid value"


Prelude> :load ffact
[1 of 1] Compiling Main             ( ffact.hs, interpreted )
Ok, modules loaded: Main.
*Main> ffact 0
1
*Main> ffact 1
1
*Main> ffact 10
3628800
*Main> ffact -1

<interactive>:1:0:
    No instance for (Num (Int -> Int))
      arising from a use of `-' at <interactive>:1:0-7
    Possible fix: add an instance declaration for (Num (Int -> Int))
    In the expression: ffact - 1
    In the definition of `it': it = ffact - 1
*Main> ffact (-1)
*** Exception: invalid value

ロジック的にはなんのおもしろみもありません。


-- 
木村浩一/KIMURA Koichi
 I thought what I'd do was, I'd pretend I was one of those deaf-mutes or
shoud I?
 mail kbk at kt.rim.or.jp
 web  www.kt.rim.or.jp/~kbk/zakkicho/index.html

-- 
木村浩一/KIMURA Koichi
 I thought what I'd do was, I'd pretend I was one of those deaf-mutes or
shoud I?
 mail kbk at kt.rim.or.jp
 web  www.kt.rim.or.jp/~kbk/zakkicho/index.html