double x = x + x
quadruple x = double (double x)
factorial n = product [1..n]
average ns = sum ns `div` length ns
element n = a `div` length xs
where
a = 10
xs = [1,2,3,4,5]
n = a `div` length xs
where
a = 10
xs = [1,2,3,4,5]
-- Last function
last' :: [x] -> Maybe x
last' [] = Nothing
last' [x] = Just x
last' (x:xs) = last' xs
['a', 'b', 'c']: [Char]
('a', 'b', 'c'): (Char, Char, Char)
[(False, '0'), (True, '1')]: [(Bool, Char)]
([False, True], ['0', '1']): ([Bool], [Char])
[tail, init, reverse]: [[a] -> [a]]
second::[a] -> a
second xs = head (tail xs)
swap::(a, b) -> (b, a)
swap (x, y) = (y, x)
pair::a -> b -> (a, b)
pair x y = (x, y)
double::Num a => a -> a
double x = x * 2
palindrome::[a] -> Bool
palindrome xs = reverse xs == xs
twice::(a -> a) -> a -> a
twice f x = f (f x)
-- 1
safeTailPattern :: [a] -> [a]
safeTailPattern [] = []
safeTailPattern (x : xs) = xs
safeTailConditional :: [a] -> [a]
safeTailConditional xs = if null xs then [] else drop 1 xs
safeTailGuard :: [a] -> [a]
safeTailGuard xs
| null xs = []
| otherwise = drop 1 xs
-- 2
(||) :: Bool -> Bool -> Bool
(||) True _ = True
(||) _ True = True
(||) False False = False
(||) :: Bool -> Bool -> Bool
(||) False False = False
(||) _ _ = True
(||) :: Bool -> Bool -> Bool
(||) True True = True
(||) _ True = True
(||) True _ = True
(||) False False = False
-- 3
(&&) :: Bool -> Bool -> Bool
(&&) a b = if a && b then True else False
(&&) :: Bool -> Bool -> Bool
(&&) a b = a == True && b
pyths :: Int -> [(Int, Int, Int)]
pyths n = [(x, y, z) | x <- [1 .. n], y <- [1 .. n], z <- [1 .. n], (x ^ 2) + (y ^ 2) == (z ^ 2)]
factors :: Int -> [Int]
factors n = [x | x <- [1 .. n], n `mod` x == 0, n /= x]
sumFactors :: Int -> Int
sumFactors = sum . factors
perfects :: Int -> [Int]
perfects n = [x | x <- [1 .. n], sumFactors x == x]
scalarProduct :: (Num a) => [a] -> [a] -> a
scalarProduct xs ys = sum [x * y | (x, y) <- zip xs ys]
and' :: [Bool] -> Bool
and' (x : xs)
| not x = False
| otherwise = x && and' xs
concat' :: [[a]] -> [a]
concat' [] = []
concat' [[]] = []
concat' xss = [x | xs <- xss, x <- xs]
replicate' :: Int -> a -> [a]
replicate' 0 _ = []
replicate' n a = a : replicate' (n - 1) a
(!!*) :: [a] -> Int -> Maybe a
(!!*) [] _ = Nothing
(!!*) (a : as) 0 = Just a
(!!*) (_ : as) n
| n < 0 = Nothing
| otherwise = (!!*) as (n - 1)
elem' :: (Eq a) => a -> [a] -> Bool
elem' _ [] = False
elem' a [x] = a == x
elem' a (_ : xs) = elem' a xs
merge' :: [Int] -> [Int] -> [Int]
merge' [] [] = []
merge' [] ys = ys
merge' xs [] = xs
merge' (x : xs) (y : ys)
| x <= y = x : merge' xs ys
| otherwise = y : merge' xs ys
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use camelCase" #-}
{-# HLINT ignore "Use map" #-}
-- higher-order functions that return function as results better known as
-- Curried Functions
list_comp :: (a -> b) -> [a] -> (a -> Bool) -> [b]
list_comp f xs p = map f (filter p xs)
foldrMap :: (a -> b) -> [a] -> [b]
foldrMap f = foldr (\x acc -> f x : acc) []
foldrFilter :: (a -> Bool) -> [a] -> [a]
foldrFilter f = foldr (\x acc -> if f x then x : acc else acc) []