-- exhl6.hs example create and use functions factorial :: (Integral a) => a -> a -- special type, multiple precision factorial n = if n < 2 then 1 else n * factorial (n - 1) -- factorial Using recursion (with pattern matching) factr :: (Integral a) => a -> a factr 0 = 1 factr n = n * factr (n - 1) -- Using recursion (with guards) factrg :: (Integral a) => a -> a factrg n | n < 2 = 1 | otherwise = n * factrg (n - 1) -- Using a list and the "product" function factp :: (Integral a) => a -> a factp n = product [1..n] calc :: String -> [Float] -- define function calc calc = foldl f [] . words where f (x:y:zs) "+" = y+x:zs f (x:y:zs) "-" = y-x:zs f (x:y:zs) "*" = y*x:zs f (x:y:zs) "/" = y/x:zs f xs y = read y : xs a = "2.0 3.0 *" b = "2.0 3.0 * 1.5 +" c = "2.0 3.0 * 3.0 0.5 * +" main :: IO () main = do print "haskell exhl6.hs running"; print "4 versions of factorial function" putStr "factorial(3) = " print (factorial(3)) putStr "factorial(20) = " print (factorial(20)) putStr "factorial(52) = " print (factorial(52)) putStr "factr(3) = " print (factr(3)) putStr "factrg(3) = " print (factrg(3)) putStr "factp(3) = " print (factp(3)) putStrLn "fact4.hs finished" print "function calc takes reverse polish expressions" putStr(a++" = ") print(calc a) putStr(b++" = ") print(calc b) putStr(c++" = ") print(calc c) print "exhl6.hs finished";