arithmetic
This commit is contained in:
parent
a7309c7473
commit
4790712b97
2 changed files with 86 additions and 0 deletions
19
factorial/Factorial.hs
Normal file
19
factorial/Factorial.hs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
module Main where
|
||||
|
||||
factorial :: Integer -> Either String Integer
|
||||
factorial n = if n < 0 then
|
||||
Left "n cannot be negative"
|
||||
else Right $ factorial' n 1 where
|
||||
factorial' 0 res = res
|
||||
factorial' n res = factorial' (n - 1) $! n * res
|
||||
|
||||
main :: IO()
|
||||
main = do
|
||||
s <- getLine
|
||||
let n = (read s :: Integer)
|
||||
let res = factorial n
|
||||
case res of
|
||||
Right r -> do
|
||||
print r
|
||||
Left err -> do
|
||||
putStrLn $ "Error: " ++ err
|
||||
Reference in a new issue