0% found this document useful (0 votes)
6 views20 pages

Haskell CHAPTER4 DefiningFunctions

This chapter discusses several methods for defining functions in Haskell, including guarded equations, conditional expressions, pattern matching, lambda expressions, and operator sections. It provides examples of using each method to define simple functions like 'not' and '&&'. Pattern matching allows functions to be defined based on the structure of their arguments. Lambda expressions allow functions to be defined anonymously without a name. Operator sections allow operators to be treated as functions by partially applying their arguments.

Uploaded by

acaronar7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views20 pages

Haskell CHAPTER4 DefiningFunctions

This chapter discusses several methods for defining functions in Haskell, including guarded equations, conditional expressions, pattern matching, lambda expressions, and operator sections. It provides examples of using each method to define simple functions like 'not' and '&&'. Pattern matching allows functions to be defined based on the structure of their arguments. Lambda expressions allow functions to be defined anonymously without a name. Operator sections allow operators to be treated as functions by partially applying their arguments.

Uploaded by

acaronar7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

CHAPTER

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

Guarded equations are an alternative method for defining functions in


programming languages. They involve using a sequence of logical
expressions, called guards, to determine which result should be returned
based on the input.
4.3 GUARDED
EQUATIONS

The main benefit of guarded equations over conditional expressions is that


definitions with multiple guards are easier to read.
4.4 PATTERN MATCHING

Many functions have a particularly clear definition using pattern


matching on their arguments.

Example 1 : ‘not’ Function


4.4 PATTERN MATCHING

Example 2 : ‘&&’ Operator


4.4 PATTERN MATCHING

Simplifying && Operator


4.4 PATTERN MATCHING

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

Creating Nameless Functions


Lambda expressions are a way to define functions without assigning them a name. Instead,
they are expressed directly in terms of their arguments. In Haskell, the syntax for a lambda
expression is ‘\argument -> body’, where ‘\’ represents the Greek letter lambda (λ),
‘argument’ is the pattern for each argument, and ‘body’ specifies how the result is
calculated.
4.5 LAMBDA EXPRESSIONS

Using Lambda Expressions


Lambda expressions can be used directly where functions are expected, just like named
functions. They can be passed as arguments to higher-order functions or used in function
compositions.
4.5 LAMBDA EXPRESSIONS

Formalizing Curried Functions


Currying is the process of converting a function that takes multiple arguments into a
sequence of functions, each taking a single argument. Lambda expressions can help
formalize the concept of currying in function definitions.
4.5 LAMBDA EXPRESSIONS

Functions Returning Functions


Lambda expressions are particularly useful when defining functions that return other
functions. This is common in functional programming paradigms.
4.5 LAMBDA EXPRESSIONS

Avoiding Naming Functions


Lambda expressions can be employed to avoid explicitly naming functions that are
referenced only once in a program. This enhances code readability and reduces clutter by
keeping the definition inline.
4.6 OPERATOR SECTIONS

Converting Operators into Functions


In Haskell, operators like +, -, *, etc., are typically written between their two arguments.
You can convert an operator into a curried function by enclosing the operator's name in
parentheses.

Example:

(+) 1 2 is equivalent to 1 + 2, where + is treated as a function taking two arguments.


4.6 OPERATOR SECTIONS

Operator Sections
Operator sections are expressions that convert operators into functions by partially
applying one or both of their arguments.

There are three types of operator sections:

• (+#) 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

A formal meaning for pattern matching by translation using


more primitive features of the language is given in the Haskell
Report [4]. The Greek letter λ used when defining nameless
functions comes from the lambda calculus [6], the
mathematical theory of functions upon which Haskell is
founded.

PRESENTATION TITLE 19
THANK YOU
PRESENTATION TITLE 20

You might also like