haskell-course

foldl, foldr, foldl'

foldr (+) 0 [1..1000000]
= 1 + (foldr (+) 0 [2..1000000])
= 1 + (2 + (foldr (+) 0 [3..1000000]))
.
.
.
= 1 + (2 + ... + (1000000 + 0)))
= 1 + (2 + ... (+ 1000000))
=
.
.
.
.
= ...
foldl (+) 0 [1..1000000]
= foldl (+) (0+1) [2..1000000]
= foldl (+) ((0+1)+2) [3..1000000]
.
.
.
= ((0 + 1) + 2) ... 1000000)
.
.
.
= ....
foldl' op init [] = init
foldl' op init (x:xs) = let z = (init `op` x) in seq z (foldl' op z xs)

Debug.Trace

myfun a b | trace ("myfun " ++ show a ++ " " ++ show b) False = undefined
myfun a b = ...
cylinder r h
  | trace ("calling cylinder with r = " ++ show r ++ " and h = " ++ show h) False =
    undefined
cylinder r h =
    let sideArea = 2 * pi * r * h  
        topArea = pi * r ^2  
    in  sideArea + 2 * topArea  

Modules

Algebraic Datatypes

type IncreasingList = [Int]

type Trace a = Int -> a

newtype Feet = Feet Double
newtype Cm   = Cm   Double
data Ellipse = MkEllipse Float Float
ellipseArea :: Ellipse -> Float
ellipseArea (MkEllipse major minor) = pi * major * minor
data Ellipse = Ellipse Float Float
data Ellipse = Ellipse {major :: Float, minor :: Float}
ellipseA = Ellipse 1.0 9.0
ellipseB = ellipseA {major = 10.0}
data Day = Saturday | Sunday | Monday | Tuesday | Wednesday | Thursday | Friday
data Shape = Rectangle Float Float | Square Float
area :: Shape -> Float
area (Rectangle a b) = a * b
area (Square a) = a * a
data List a = Nil | Cons a (List a)
data NonEmpty a = Singleton a | More a (NonEmpty a)
data Stream a = Stream { head :: a, tail :: (Stream a) }
data Mealy a b = Mealy { mOut :: a -> b, mNext :: a -> Mealy a b }
ones :: Stream Int
ones = Stream 1 ones
mealySimple :: Mealy Int String
mealySimple = Mealy { mOut = show, mNext = \x -> mealySimple }
lookup :: Int -> [(Int, b)] -> b
lookup i tab = snd . head $ filter (\(x,y) -> x ==  i) tab
lookup _ [] = Nothing
lookup i (k, v):xs
  | i == k    = Just v
  | otherwise = lookup i xs

Kinds

Typeclasses

data Ellipse = Ellipse {major :: Float, minor :: Float}

instance Show Ellipse where
    show (Ellipse major minor)
        | major == minor = "Just a circle"
        | otherwise = "Another ellipse"
myLookup :: Int -> [(Int, b)] -> Maybe b
myLookup _ [] = Nothing
myLookup i (k, v):xs
  | i == k    = Just v
  | otherwise = lookup i xs
quicksort (x : xs) = lesser ++ [x] ++ greater
  where
    lesser = [y | y <- xs, y <= x]
    greater = [y | y <- xs, y >= x]
data Two = One | Two deriving (Ord, Show)

testCompare = [One, Two] < [One, One]