{-
addOneToAll :: [Int] -> [Int]
addOneToAll [] = []
addOneToAll (h : t) = (h+1: addOneToAll t)
squareAll :: [Int] -> [Int]
squareAll [] = []
squareAll (h : t) = ( h*h : squarAll t)
-}
data IntList = Empty
| Cons Int IntList
deriving Show
myIntList = Cons 2 (Cons (-3) (Cons 5 Empty))
-------------------- recursion
{-
addOneToAll :: IntList -> IntList
addOneToAll Empty = Empty
addOneToAll (Cons x xs) = Cons (x + 1) (addOneToAll xs)
absAll :: IntList -> IntList
absAll Empty = Empty
absAll (Cons x xs) = Cons (abs x) (absAll xs)
squareAll :: IntList -> IntList
squareAll Empty = Empty
squareAll (Cons x xs) = Cons (x * x) (squareAll xs)
-}
------------------ recursion patterns
mapIntList :: (Int -> Int) -> IntList -> IntList
mapIntList _ Empty = Empty
mapIntList f (Cons x xs) = Cons (f x) (mapIntList f xs)
addOne x = x + 1
square x = x * x
addOneToAll xs = mapIntList addOne xs
absAll xs = mapIntList abs xs
squareAll xs = mapIntList square xs
------------------ filter
{-
keepOnlyEven :: IntList -> IntList
keepOnlyEven Empty = Empty
keepOnlyEven (Cons x xs)
| even x = Cons x (keepOnlyEven xs)
| otherwise = keepOnlyEven xs
-}
------------------ filter pattern
filterIntList :: (Int -> Bool) -> IntList -> IntList
filterIntList _ Empty = Empty
filterIntList p (Cons x xs)
| p x = Cons x (filterIntList p xs)
| otherwise = filterIntList p xs