home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacHaskell 2.2 / progs / demo / fact.hs < prev    next >
Encoding:
Text File  |  1994-09-27  |  436 b   |  18 lines  |  [TEXT/YHS2]

  1. {- This is a simple factorial program which uses the I/O system
  2.    to read the input and print the result -}
  3.  
  4. module Main where
  5.  
  6. fact :: Integer -> Integer    
  7. fact 0 = 1
  8. fact (n+1) = (n+1)*fact n
  9. fact _ = error "Negative argument to factorial"
  10.  
  11. main = putStr "Type in N: " >>
  12.        getLine >>= \l ->
  13.          case reads l of
  14.            [(x,"")] -> putStr (l ++ "! = " ++ show (fact x))
  15.            _        -> putStr "Eh?\n" >> main
  16.  
  17.  
  18.