Haskell CHAPTER4 DefiningFunctions
Haskell CHAPTER4 DefiningFunctions
4
DEFINING
FUNCTION
S
Ann Kristil P. Concillado
Ivy Grace R. Bisnar
Charlyn A. Borong
4.1 NEW FROM
OLD
4.2 CONDITIONAL EXPRESSIONS
4.3 GUARDED
EQUATIONS
Tuple patterns
4.4 PATTERN MATCHING
List patterns
Similarly, a list of patterns is itself a pattern, which matches any list of the same length whose
elements all match the corresponding patterns in order. For example, a function test that decides if
a list contains precisely three characters beginning with the letter ’a’ can be defined as follows:
4.4 PATTERN MATCHING
Cons Patterns
Up to this point, we have viewed lists as a primitive notion in Haskell. In fact they are not primitive
as such, but are constructed one element at a time starting from the empty list [] using an operator :
called cons that constructs a new list by prepending a new element to the start of an existing list.
As well as being used to construct lists, the cons operator can also be used to construct patterns,
which match any non-empty list whose first and remaining elements match the corresponding
patterns in order. For example, we can now define a more general version of the function test that
decides if a list containing any number of characters begins with the letter ’a’:
Note that cons patterns must be parenthesised, because function application has higher priority than all other operators in the language.
4.5 LAMBDA EXPRESSIONS
Example:
Operator Sections
Operator sections are expressions that convert operators into functions by partially
applying one or both of their arguments.
• (+#) or (# y): Convert the operator into a function where the first argument is fixed.
• (x #): Convert the operator into a function where the second argument is fixed.
• (#): Convert the operator into a function with both arguments unspecified.
SUMMARY
PRESENTATION TITLE 19
THANK YOU
PRESENTATION TITLE 20