Factorial in Haskell
            Example for versions 
            
                GHC 6.10.4
            
            
                
                        
	        
        
    
This example performs a “fold” using the function foldl (which is a left-fold, but since multiplication is associative, left fold and right fold are the same) to fold multiplication over the list [1..n]. We provide the “initial value” 1, so that it will produce 1 when the list is empty.
module Main where
factorial :: Integer -> Integer
factorial n = foldl (*) 1 [1..n]
line x = putStrLn $ show x ++ "! = " ++ factorial x
main = mapM_ line [0..16]
Comments
]]>blog comments powered by Disqus
]]>