Programming in Haskell: Chapter 4 - Defining Functions
Programming in Haskell: Chapter 4 - Defining Functions
1
Conditional Expressions
Note:
abs n | n ≥ 0 = n
| otherwise = -n
signum n | n < 0 = -1
| n == 0 = 0
| otherwise = 1
Note:
True && b = b
False && _ = False
Note:
_ && _ = False
True && True = True
b && b = b
_ && _ = False
9
List Patterns
[1,2,3,4]
Means 1:(2:(3:(4:
[]))).
10
Functions on lists can be defined using x:xs patterns.
head :: [a] a
head (x:_) = x
> head []
*** Exception: empty list
head x:_ = x
12
Lambda Expressions
x x + x
14
Why Are Lambda's Useful?
means
For example:
can be simplified to
For example:
> 1+2
3
> (+) 1 2
3
17
This convention also allows one of the arguments
of the operator to be included in the parentheses.
For example:
> (1+) 2
3
> (+2) 1
3
True && b = b
False && _ = False
21