Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Задача №1
- factorial1 :: Int -> Int
- factorial1 x = if x == 0 then 1 else x * factorial1 (x - 1)
- -- Задача №2
- factorial2 :: Int -> Int
- factorial2 0 = 1
- factorial2 x = x * factorial2 (x - 1)
- -- Задача №3
- doubleFact :: Int -> Int
- doubleFact x = if x <= 2 then max 1 x else x * doubleFact (x - 2)
- -- Задача №4
- factorial4 :: Int -> Int
- factorial4 0 = 1
- factorial4 x = if x < 0 then error "arg must be >=0" else x * factorial4 (x - 1)
- -- Задача №5
- --1-ый способ:
- factorial51 :: Int -> Int
- factorial51 x
- | x >= 0 = if x == 0 then 1 else x * factorial51 (x - 1)
- | otherwise = error "arg must be >=0"
- --2-ой способ:
- factorial52 :: Int -> Int
- factorial52 x
- | x > 0 = x * factorial52 (x - 1)
- | x == 0 = 1
- | otherwise = error "arg must be >=0"
- -- Задача №6
- --1-ый способ:
- factorial61 :: Int -> Int
- factorial61 x
- | x > 0 = doubleFact x * doubleFact (x - 1)
- | x == 0 = 1
- | otherwise = error "arg must be >=0"
- --2-ой способ:
- factorial62 :: Int -> Int
- factorial62 x | x >= 0 = helper 1 x
- | otherwise = error "arg must be >=0"
- helper :: Int -> Int -> Int
- helper acc 0 = acc
- helper acc x = helper (acc * x) (x - 1)
- --3-ий способ:
- factorial63 :: Int -> Int
- factorial63 n = aux n 1
- where
- aux n acc
- | n <= 1 = acc
- | otherwise = aux (n - 1) (n * acc)
Add Comment
Please, Sign In to add comment