FP101x - week 3 Defining Functions


Conditional Expressions

Guarded Equations

 abs n | n >= 0     = n
          | otherwise = -n

 signum n | n < 0 = -1
                    | n == 0 =0
                    | otherwise = 1

Pattern Matching

so..

List Pattern

head :: [a] -> a
head (x:_) = x
tail :: [a] -> [a]
tail (_:xs) = xs
> head []
ERROR

Lambda Expressions

ex)

add x y = x + y

means

add = \x -> (\y -> x+y)

ex)

const :: a -> b -> a
const x _ = x

to lambda

const :: a -> ( b -> a )
const x = \_ -> x

Sections

> 1+ 2
3
> (+) 1 2
3
> (1+) 2
3
> (+2) 1
3