design: one way is to make it so simple that there are obviously no deficiencies; the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult
add :: (Num a) => a -> a -> a add x y = x + y PARTIAL APPLICATION IN HASKELL ALL FUNCTIONS TAKE ONLY ONE ARGUMENT AND RETURN A VALUE OR A NEW FUNCTION OF ONE ARGUMENT add1 :: (Num a) => a -> a add1 = add 1
MULTIPLICATION data Bool = False | True data Point = Point Float Float data Shape = Circle Point Point | Rectangle Point Point EVERY ALGEBRAIC DATA TYPE IS A TREE WITH NODES AS COMPOSITE DATA TYPE AND LEAFS AS PRIMITIVE DATA TYPE
Int factorial 0 = 1 factorial n = n * factorial (n - 1) head :: [ a ] -> a head [ ] = error "head: empty list" head (x:_) = x FUNCTIONS BUILD NEW TREES FROM INPUT TREES USING DECONSTRUCTION (PATTERN MATCHING) AND COMPOSITION (EXPRESSIONS) function deconstruction = composition function pattern matching = expressions
a ] -> [ b ] map _ [ ] = [ ] map f (x : xs) = f x : map f xs FILTER filter :: (a -> Bool) -> [ a ] -> [ a ] filter _ [ ] = [ ] filter p (x : xs) | p x = x : filter p xs | otherwise = filter p xs REDUCE reduce :: (a -> b -> a) -> a -> [ b ] -> a reduce _ z [ ] = z reduce f z (x : xs) = reduce f (f z x) xs
product = reduce (*) 1 MAP AS REDUCE map :: (a -> b) -> [ a ] -> [ b ] map f = reduce ((:) . f) [ ] FILTER AS REDUCE filter :: (a -> Bool) -> [ a ] -> [ a ] filter p = reduce (\ x -> if p x then (x :) else id) [ ]
Just a TYPE VARIABLE AND POLYMORPHIC FUNCTION head :: [ a ] -> Maybe a head [ ] = Nothing head (x:_) = Just x TYPE CLASS class Eq a where (==) :: a -> a -> Bool INSTANCE OF TYPE CLASS instance Eq Integer where x == y = x ` ` y eq
STATEMENT EVENT-BASED OVER MULTITHREADED CONTEXT DECLARATIVE EXPRESSION LOCAL VARIABLES WHERE LET ... IN ... CONDITIONALS GUARDS IF ... THEN ... ELSE ... DECONSTRUCTION PATTERN MATCHING CASE ... OF ... FUNCTION DEFINITION EQUATIONS LAMBDA FUNCTIONS