You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

20 lines
476 B
Haskell

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