-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
27 lines (24 loc) · 839 Bytes
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- option 1 (https://github.com/PiotrJustyna/haskell-anywhere):
-- ./ghci.bat C:\Users\piotr_justyna\Documents\github\programming-in-haskell\part1_chapter6_exercise1
-- option 2 (stack):
-- stack ghci
-- option 3 (ghci):
-- ghci
--
-- :load Main
main = do
putStrLn "factorial 3"
putStrLn . show $ factorial 3
putStrLn "factorial 0"
putStrLn . show $ factorial 0
putStrLn "factorial -3"
putStrLn . show $ factorial (-3)
-- How does the recursive version of the factorial function behave
-- if applied to a negative argument, such as (-1)?
-- Modify the definition to prohibit negative arguments
-- by adding a guard to the recursive case.
factorial :: Int -> Int
factorial 0 = 1
factorial n
| n < 0 = error "Negative arguments are not expected."
| otherwise = n * factorial (n-1)