{-# OPTIONS_GHC -Wall -fno-warn-type-defaults #-}
module Lec3 where
import Prelude hiding (map, foldr, filter, pred, sum, product)
import Data.Char
import Test.HUnit
anotherFour :: Int
anotherFour = doTwice (+2) 0
doTwice :: (a -> a) -> a -> a
doTwice f x = f $ f x
greaterThan10 :: Int -> Bool
greaterThan10 = (10 <)
ex1 :: (a -> a) -> a -> a
ex1 = doTwice doTwice
len :: [a] -> Int
len [] = 0
len (_:xs) = 1 + len xs
toUpperString :: String -> String
toUpperString [] = []
toUpperString (c:cs) = toUpper c : toUpperString cs
type XY = (Double, Double)
type Polygon = [XY]
shiftXY :: XY -> XY -> XY
shiftXY (dx, dy) (x, y) = (x + dx, y + dy)
shiftPoly :: XY -> Polygon -> Polygon
shiftPoly _ [] = []
shiftPoly d (xy:xys) = shiftXY d xy : shiftPoly d xys
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = (f x) : (map f xs)
toUpperString' :: String -> String
toUpperString' = map toUpper
shiftPoly' :: XY -> Polygon -> Polygon
shiftPoly' d = map (shiftXY d)
listIncr :: [Int] -> [Int]
listIncr [] = []
listIncr (x:xs) = (x+1) : listIncr xs
listIncr' :: [Int] -> [Int]
listIncr' = map (+1)
sum :: [Int] -> Int
sum [] = 0
sum (x:xs) = x + (sum xs)
product :: [Int] -> Int
product [] = 1
product (x:xs) = x * (product xs)
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr _ base [] = base
foldr f base (x:xs) = f x (foldr f base xs)
sum', product' :: [Int] -> Int
sum' = foldr (+) 0
product' = foldr (*) 1
len' :: [a] -> Int
len' = foldr (\_ acc -> 1 + acc) 0
factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n-1)
factorial' :: Int -> Int
factorial' n = foldr (*) 1 [1..n]
filter :: (a -> Bool) -> [a] -> [a]
filterTests :: Test
filterTests = TestList
[ filter (>10) [1..20] ~?= [11..20],
filter (\l -> sum l <= 42) [ [10,20], [50,50], [1..5] ]
~?= [[10,20],[1..5]] ]
filter pred = foldr (\a rest -> if pred a then a : rest else rest) []
main :: IO ()
main = putStrLn "This is Lec3"