SlideShare uma empresa Scribd logo
1 de 32
Baixar para ler offline
Real World Haskell:
     Lecture 2

   Bryan O’Sullivan


     2009-10-14
My homework, using only concepts from last week
   import Data . L i s t ( i s I n f i x O f )

   pattern = ” toast ”

   f o r m a t number l i n e = show number ++ ” : ” ++ l i n e

   g r e p number i n p u t
          = i f null input
            then [ ]
            e l s e i f i s I n f i x O f p a t t e r n ( head i n p u t )
                    then        f o r m a t number ( head i n p u t )
                             : g r e p ( number + 1 ) ( t a i l i n p u t )
                    else        g r e p ( number + 1 ) ( t a i l i n p u t )

   grepFromOne i n p u t = u n l i n e s ( g r e p 1 ( l i n e s i n p u t ) )

   main = i n t e r a c t grepFromOne
Wasn’t Haskell supposed to be “pretty”?




   That grep function sure didn’t look pretty to me!

   But what, specifically, is ugly about it?
       We repeat ourselves, using head and tail twice.
       There’s a mess of nested if /else badness going on.
Lists, revisited



   There are two ways to construct a list:
        An empty list
        []
        A non-empty list
        firstElement : restOfList

   We refer to [] and : as list constructors, since they construct list
   values.
Lists, constructed


   Knowing about these constructors, how might we construct a
   4-element list?
Lists, constructed


   Knowing about these constructors, how might we construct a
   4-element list?
       1 : 2 : 3 : 4 : []

   The bracketed notation we saw last week is syntactic sugar for the
   form above.

   In other words, any time you see this:
        [1,2,3,4]
   You can read it as this, and vice versa:
       1 : 2 : 3 : 4 : []
Lists, misconstrued



   Beginner mistake alert:
   A list must end with an empty list. So a construction like this
   makes no sense:
        ’a’ : ’b’ : ’c’

   How would we fix it up?
        ’a’ : ’b’ : ’c’ : []
Back to our roots


   Remember the fragment of square root code from last week?
   oneRoot a b c = (−b + ( b ˆ2 + 4∗ a ∗ c ) ) / ( 2 ∗ a )

   If we pass in a value of zero for a, the root is undefined, since we’d
   be dividing by zero.

   oneRoot a b c = i f a == 0
                   then (−b + ( b ˆ2 − 4∗ a ∗ c ) )
                           / (2∗ a )
                   e l s e e r r o r ” d i v i d e by z e r o ! ”
But...



   I don’t like that if , because how would we write this using
   mathematical notation?


                           −b ± (b 2 − 4ac)
           roots(a, b, c) =                            if a = 0
                                 2a
                         = undefined                  otherwise

   And . . . isn’t Haskell supposed to be mathematically inspired?
Introducing guards

   A guard is a Boolean expression preceded by a vertical bar
   character.

   oneRoot a b c
     | a /= 0            = (−b + ( b ˆ2 − 4∗ a ∗ c ) ) / ( 2 ∗ a )
     | o t h e r w i s e = e r r o r ” d i v i d e by z e r o ”


       Guards are evaluated in top-to-bottom order.
       For the first one that evaluates to True, the expression on the
       right of the = sign is used as the result of the function.
       The name otherwise is simply another name for True.
Using guards


   Here’s a second attempt at our grep function, this time using
   guarded expressions:
   g r e p number i n p u t
       | null input
             =        []
       | i s I n f i x O f p a t t e r n ( head i n p u t )
             =        f o r m a t number ( head i n p u t )
                  : g r e p ( number + 1 ) ( t a i l i n p u t )
       | otherwise
             =        g r e p ( number + 1 ) ( t a i l i n p u t )
How did this help?




   We got rid of the nested if expressions, and our “flatter” code is
   easier to follow.

   It’s still fugly and repetitive, though. What about head and tail ?
Pattern matching




   When we construct a list, the Haskell runtime has to remember
   what constructors we used.

   It goes a step further, and makes this information available to us.

   We can examine the structure of a piece of data at runtime using
   pattern matching.
Pattern matching on an empty list



   What’s the length of an empty list?

   myLength [ ] = 0

   This is a function of one argument.
   If that argument matches the empty-list constructor, our function
   returns the value 0.
Pattern matching on a non-empty list



   What’s the length of a non-empty list?

   myLength ( x : x s ) = 1 + myLength x s

   If our argument matches the non-empty-list constructor “:”, then:
       the head of the list is bound to the name x;
       the tail to xs;
       and the expression is returned with those bindings.
Aaaand it’s over to you


   Now that we know how pattern matching works, let’s do some
   super-simple exercises:

   Write versions of the head and tail functions:

   head     [1 ,2 ,3]
     == >   1
   tail     [ ’a’ , ’b’ , ’c ’]
     == >   [ ’b’ , ’c ’]

   Give your versions different names, or you’ll have a hard time
   trying them out in ghci.
Matching alternative patterns


   We combine our two pattern matches into one function definition
   by writing them one after the other:

   myLength [ ]         = 0
   myLength ( x : x s ) = 1 + myLength x s

   As with guards, pattern matching proceeds from top to bottom
   and stops at the first success.
       The RHS of the first pattern that succeeds is used as the
       body of the function.
Matching alternative patterns


   We combine our two pattern matches into one function definition
   by writing them one after the other:

   myLength [ ]         = 0
   myLength ( x : x s ) = 1 + myLength x s

   As with guards, pattern matching proceeds from top to bottom
   and stops at the first success.
       The RHS of the first pattern that succeeds is used as the
       body of the function.

   Question: What do you suppose happens if no pattern matches?
Over to you, part two

   And now that we know how to write function definitions that can
   deal with multiple patterns, another exercise:

   Write a version of the take function:

   take 3    [100 ,200 ,300 ,400 ,500]
     ==>     [100 ,200 ,300]
   take 3    [ ’a’ , ’b ’]
     ==>     [ ’a’ , ’b ’]
   take 3    []
     ==>     ???
Over to you, part two

   And now that we know how to write function definitions that can
   deal with multiple patterns, another exercise:

   Write a version of the take function:

   take 3    [100 ,200 ,300 ,400 ,500]
     ==>     [100 ,200 ,300]
   take 3    [ ’a’ , ’b ’]
     ==>     [ ’a’ , ’b ’]
   take 3    []
     ==>     ???

   Now use ghci to figure out what the drop function does, and
   write a version of that.
Metasyntactic variables


   Languages have their cultural habits, and Haskell is no exception.

   You’ll very often see the names used when pattern matching a list
   follow a naming convention like this:
       (x: xs)
       (y: ys)
       (d:ds)
   and so on.

   Think of the “s” suffix as “pluralizing” a name, so “x” (ex) is the
   head of the list, and “xs” (exes) is the rest.
Matching multiple patterns




   We can match more than one pattern at a time.

   Consider how we might add the elements of two vectors,
   represented as lists:

   sumVec ( x : x s ) ( y : y s ) = x + y : sumVec x s y s
   sumVec [ ]         []          = []
Combining pattern matching and guards


   Things start to get seriously expressive when we combine language
   features.

   Remember that bloated grep definition from earlier? Let’s put our
   new friends to work!

   grep n [ ]                      =      []
   grep n ( x : xs )
     | i s I n f i x O f pattern x =    format n x
                                      : g r e p ( n+1) x s
      | otherwise                   =   g r e p ( n+1) x s
What’s happening here?

   When we define a function, a pattern binds names to values. Given
   a list and a pattern (x: xs), if the list is non-empty, then x is bound
   to its head, and xs to its tail.
        Then each guard (if any) associated with that pattern is
        evaluated in turn, with those bindings in effect, until a guard
        succeeds.
        Once a guard succeeds, its RHS is used as the result, with the
        bindings from that pattern still in effect.
        If the pattern match fails, or no guard succeeds, we fall
        through to the next pattern and its guards.
What’s happening here?

   When we define a function, a pattern binds names to values. Given
   a list and a pattern (x: xs), if the list is non-empty, then x is bound
   to its head, and xs to its tail.
        Then each guard (if any) associated with that pattern is
        evaluated in turn, with those bindings in effect, until a guard
        succeeds.
        Once a guard succeeds, its RHS is used as the result, with the
        bindings from that pattern still in effect.
        If the pattern match fails, or no guard succeeds, we fall
        through to the next pattern and its guards.

   Note: If all patterns and guards in a function definition were to fail
   on some input, we’d get a runtime error. That would be bad.
And speaking of bad. . .



   Remember our sumVec function?

   sumVec ( x : x s ) ( y : y s ) = x + y : sumVec x s y s
   sumVec [ ]         []          = []

   What happens if we apply this to lists of different lengths?

       sumVec [1,2,3] [4,5,6,7,8]
And speaking of bad. . .



   Remember our sumVec function?

   sumVec ( x : x s ) ( y : y s ) = x + y : sumVec x s y s
   sumVec [ ]         []          = []

   What happens if we apply this to lists of different lengths?

       sumVec [1,2,3] [4,5,6,7,8]

   So . . . what can we do about that exciting behaviour?
One possible response

   Let’s declare that the sum of two vectors should end when we
   reach the end of the shorter vector.

   sumVec ( x : x s ) ( y : y s ) = x + y : sumVec x s y s
   sumVec what        ever        = []

   Whoa, dude. . . Why does this work?
One possible response

   Let’s declare that the sum of two vectors should end when we
   reach the end of the shorter vector.

   sumVec ( x : x s ) ( y : y s ) = x + y : sumVec x s y s
   sumVec what        ever        = []

   Whoa, dude. . . Why does this work?

       The names “what” and “ever” are patterns.
       However, a plain name (with no constructors in sight) does
       not inspect the structure of its argument.
       So “what” and “ever” will each happily match either an
       empty or a non-empty list.
An aside: strings are lists

   In Haskell, we write characters surrounded by single quotes, and
   strings in double quotes. Strings are lists, so:

        ” abc ”

   is syntactic sugar for

        [ ’ a ’ , ’b ’ , ’c ’ ]

   and hence for

        ’a ’ : ’b ’ : ’c ’ : [ ]

   Functions that can manipulate lists can thus manipulate strings.
   Oh, and escape sequences such as ”rnt” work, too.
We are not limited to one constructor per pattern
   Suppose we want to squish consecutive repeats of an element in a
   list.
   compress ” f o o o b a r r r r r r ”
     == ” f o b a r ”
       >

   We can write a function to do this using an elegant combination of
   pattern matching and guards:

   compress ( x : y        : ys )
       | x == y            =      compress ( y : ys )
       | otherwise         = x : compress ( y : ys )
   compress ys             = ys

   Notice that our pattern matches on two consecutive list
   constructors!
Homework
     Write a function that returns the nth element of a list,
     counting from zero.
     nth 2 ” squeak ”
       == ’ u ’
         >

     Write a function that returns the element immdiately before
     the last element of a list.
     lastButOne [1 ,2 ,3 ,4 ,5]
       == 4
         >

     Write a function that determines whether its input is a
     palindrome.
     isPalindrome ” foobar ”
       == F a l s e
         >
     isPalindrome ” foobarraboof ”
       == True
         >

Mais conteúdo relacionado

Mais procurados

Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Philip Schwarz
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Philip Schwarz
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskellgoncharenko
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data scienceJohn Cant
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...Philip Schwarz
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Left and Right Folds - Comparison of a mathematical definition and a programm...
Left and Right Folds- Comparison of a mathematical definition and a programm...Left and Right Folds- Comparison of a mathematical definition and a programm...
Left and Right Folds - Comparison of a mathematical definition and a programm...Philip Schwarz
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldPhilip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Philip Schwarz
 
BayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBryan O'Sullivan
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Philip Schwarz
 
DEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellDEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellBryan O'Sullivan
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1Philip Schwarz
 
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Philip Schwarz
 
Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’ Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’ Philip Schwarz
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Philip Schwarz
 

Mais procurados (20)

Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Left and Right Folds - Comparison of a mathematical definition and a programm...
Left and Right Folds- Comparison of a mathematical definition and a programm...Left and Right Folds- Comparison of a mathematical definition and a programm...
Left and Right Folds - Comparison of a mathematical definition and a programm...
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and Fold
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
 
BayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore Haskell
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
 
DEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellDEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World Haskell
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1
 
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
 
Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’ Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2
 

Destaque

Coverage Recognition 1
Coverage Recognition 1Coverage Recognition 1
Coverage Recognition 1Tom Neuman
 
Les tic a l'educació. una nova oportunitat per al canvi
Les tic a l'educació. una nova oportunitat per al canviLes tic a l'educació. una nova oportunitat per al canvi
Les tic a l'educació. una nova oportunitat per al canviGemma Tur
 
Test zagadki zwierzeta
Test zagadki zwierzetaTest zagadki zwierzeta
Test zagadki zwierzetaEwaB
 
Measuring Social Media 20091022
Measuring Social Media 20091022Measuring Social Media 20091022
Measuring Social Media 20091022tonnitommi
 
TRG – Social Media For Retailers
TRG – Social Media For RetailersTRG – Social Media For Retailers
TRG – Social Media For RetailersJaci Russo
 
[2009] Fisl10 T Learning
[2009] Fisl10 T Learning[2009] Fisl10 T Learning
[2009] Fisl10 T LearningUFPE
 
Fulfillment of Interdisciplinary Study
Fulfillment of Interdisciplinary StudyFulfillment of Interdisciplinary Study
Fulfillment of Interdisciplinary StudyAMTR
 
Eportfolios and PLEs in Teacher Education. First results.
Eportfolios and PLEs in Teacher Education. First results.Eportfolios and PLEs in Teacher Education. First results.
Eportfolios and PLEs in Teacher Education. First results.Gemma Tur
 
Global Climatic Change - Engineers Perspective
Global Climatic Change - Engineers PerspectiveGlobal Climatic Change - Engineers Perspective
Global Climatic Change - Engineers PerspectiveAshok Ghosh
 
Critical Mass Forrester 09: Marketing Vegas in a Recession
Critical Mass Forrester 09: Marketing Vegas in a RecessionCritical Mass Forrester 09: Marketing Vegas in a Recession
Critical Mass Forrester 09: Marketing Vegas in a RecessionCritical Mass
 
Trg B2B and Social Media
Trg B2B and Social MediaTrg B2B and Social Media
Trg B2B and Social MediaJaci Russo
 
Walentynki
WalentynkiWalentynki
WalentynkiEwaB
 
Medical Self-Care Issue # 2 (1977)
Medical Self-Care Issue # 2 (1977)Medical Self-Care Issue # 2 (1977)
Medical Self-Care Issue # 2 (1977)Gilles Frydman
 
Задача1
Задача1Задача1
Задача1sergeeva
 
Lesson From Butterfly
Lesson From ButterflyLesson From Butterfly
Lesson From Butterflyyasir javed
 
Vic Plant Room Nz Water, Ieq Targets
Vic Plant Room Nz Water, Ieq TargetsVic Plant Room Nz Water, Ieq Targets
Vic Plant Room Nz Water, Ieq Targetstwbishop
 
Melinda: Methods and tools for Web Data Interlinking
Melinda: Methods and tools for Web Data InterlinkingMelinda: Methods and tools for Web Data Interlinking
Melinda: Methods and tools for Web Data InterlinkingFrançois Scharffe
 

Destaque (20)

Coverage Recognition 1
Coverage Recognition 1Coverage Recognition 1
Coverage Recognition 1
 
Les tic a l'educació. una nova oportunitat per al canvi
Les tic a l'educació. una nova oportunitat per al canviLes tic a l'educació. una nova oportunitat per al canvi
Les tic a l'educació. una nova oportunitat per al canvi
 
Test zagadki zwierzeta
Test zagadki zwierzetaTest zagadki zwierzeta
Test zagadki zwierzeta
 
Measuring Social Media 20091022
Measuring Social Media 20091022Measuring Social Media 20091022
Measuring Social Media 20091022
 
TRG – Social Media For Retailers
TRG – Social Media For RetailersTRG – Social Media For Retailers
TRG – Social Media For Retailers
 
Emg pilates
Emg pilatesEmg pilates
Emg pilates
 
[2009] Fisl10 T Learning
[2009] Fisl10 T Learning[2009] Fisl10 T Learning
[2009] Fisl10 T Learning
 
Fulfillment of Interdisciplinary Study
Fulfillment of Interdisciplinary StudyFulfillment of Interdisciplinary Study
Fulfillment of Interdisciplinary Study
 
Eportfolios and PLEs in Teacher Education. First results.
Eportfolios and PLEs in Teacher Education. First results.Eportfolios and PLEs in Teacher Education. First results.
Eportfolios and PLEs in Teacher Education. First results.
 
Global Climatic Change - Engineers Perspective
Global Climatic Change - Engineers PerspectiveGlobal Climatic Change - Engineers Perspective
Global Climatic Change - Engineers Perspective
 
Silver Jewellery
Silver JewellerySilver Jewellery
Silver Jewellery
 
Critical Mass Forrester 09: Marketing Vegas in a Recession
Critical Mass Forrester 09: Marketing Vegas in a RecessionCritical Mass Forrester 09: Marketing Vegas in a Recession
Critical Mass Forrester 09: Marketing Vegas in a Recession
 
Art i dona II
Art i dona IIArt i dona II
Art i dona II
 
Trg B2B and Social Media
Trg B2B and Social MediaTrg B2B and Social Media
Trg B2B and Social Media
 
Walentynki
WalentynkiWalentynki
Walentynki
 
Medical Self-Care Issue # 2 (1977)
Medical Self-Care Issue # 2 (1977)Medical Self-Care Issue # 2 (1977)
Medical Self-Care Issue # 2 (1977)
 
Задача1
Задача1Задача1
Задача1
 
Lesson From Butterfly
Lesson From ButterflyLesson From Butterfly
Lesson From Butterfly
 
Vic Plant Room Nz Water, Ieq Targets
Vic Plant Room Nz Water, Ieq TargetsVic Plant Room Nz Water, Ieq Targets
Vic Plant Room Nz Water, Ieq Targets
 
Melinda: Methods and tools for Web Data Interlinking
Melinda: Methods and tools for Web Data InterlinkingMelinda: Methods and tools for Web Data Interlinking
Melinda: Methods and tools for Web Data Interlinking
 

Semelhante a Real World Haskell: Lecture 2

Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about lazinessJohan Tibell
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 
Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Hakka Labs
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 
Understanding the "Chain Rule" for Derivatives by Deriving Your Own Version
Understanding the "Chain Rule" for Derivatives by Deriving Your Own VersionUnderstanding the "Chain Rule" for Derivatives by Deriving Your Own Version
Understanding the "Chain Rule" for Derivatives by Deriving Your Own VersionJames Smith
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
List-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesList-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesWim Vanderbauwhede
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 

Semelhante a Real World Haskell: Lecture 2 (20)

Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
I1
I1I1
I1
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
 
Understanding the "Chain Rule" for Derivatives by Deriving Your Own Version
Understanding the "Chain Rule" for Derivatives by Deriving Your Own VersionUnderstanding the "Chain Rule" for Derivatives by Deriving Your Own Version
Understanding the "Chain Rule" for Derivatives by Deriving Your Own Version
 
Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
Matlab algebra
Matlab algebraMatlab algebra
Matlab algebra
 
List-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesList-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic Languages
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Programming Hp33s talk v3
Programming Hp33s talk v3Programming Hp33s talk v3
Programming Hp33s talk v3
 

Último

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 

Último (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Real World Haskell: Lecture 2

  • 1. Real World Haskell: Lecture 2 Bryan O’Sullivan 2009-10-14
  • 2. My homework, using only concepts from last week import Data . L i s t ( i s I n f i x O f ) pattern = ” toast ” f o r m a t number l i n e = show number ++ ” : ” ++ l i n e g r e p number i n p u t = i f null input then [ ] e l s e i f i s I n f i x O f p a t t e r n ( head i n p u t ) then f o r m a t number ( head i n p u t ) : g r e p ( number + 1 ) ( t a i l i n p u t ) else g r e p ( number + 1 ) ( t a i l i n p u t ) grepFromOne i n p u t = u n l i n e s ( g r e p 1 ( l i n e s i n p u t ) ) main = i n t e r a c t grepFromOne
  • 3. Wasn’t Haskell supposed to be “pretty”? That grep function sure didn’t look pretty to me! But what, specifically, is ugly about it? We repeat ourselves, using head and tail twice. There’s a mess of nested if /else badness going on.
  • 4. Lists, revisited There are two ways to construct a list: An empty list [] A non-empty list firstElement : restOfList We refer to [] and : as list constructors, since they construct list values.
  • 5. Lists, constructed Knowing about these constructors, how might we construct a 4-element list?
  • 6. Lists, constructed Knowing about these constructors, how might we construct a 4-element list? 1 : 2 : 3 : 4 : [] The bracketed notation we saw last week is syntactic sugar for the form above. In other words, any time you see this: [1,2,3,4] You can read it as this, and vice versa: 1 : 2 : 3 : 4 : []
  • 7. Lists, misconstrued Beginner mistake alert: A list must end with an empty list. So a construction like this makes no sense: ’a’ : ’b’ : ’c’ How would we fix it up? ’a’ : ’b’ : ’c’ : []
  • 8. Back to our roots Remember the fragment of square root code from last week? oneRoot a b c = (−b + ( b ˆ2 + 4∗ a ∗ c ) ) / ( 2 ∗ a ) If we pass in a value of zero for a, the root is undefined, since we’d be dividing by zero. oneRoot a b c = i f a == 0 then (−b + ( b ˆ2 − 4∗ a ∗ c ) ) / (2∗ a ) e l s e e r r o r ” d i v i d e by z e r o ! ”
  • 9. But... I don’t like that if , because how would we write this using mathematical notation? −b ± (b 2 − 4ac) roots(a, b, c) = if a = 0 2a = undefined otherwise And . . . isn’t Haskell supposed to be mathematically inspired?
  • 10. Introducing guards A guard is a Boolean expression preceded by a vertical bar character. oneRoot a b c | a /= 0 = (−b + ( b ˆ2 − 4∗ a ∗ c ) ) / ( 2 ∗ a ) | o t h e r w i s e = e r r o r ” d i v i d e by z e r o ” Guards are evaluated in top-to-bottom order. For the first one that evaluates to True, the expression on the right of the = sign is used as the result of the function. The name otherwise is simply another name for True.
  • 11. Using guards Here’s a second attempt at our grep function, this time using guarded expressions: g r e p number i n p u t | null input = [] | i s I n f i x O f p a t t e r n ( head i n p u t ) = f o r m a t number ( head i n p u t ) : g r e p ( number + 1 ) ( t a i l i n p u t ) | otherwise = g r e p ( number + 1 ) ( t a i l i n p u t )
  • 12. How did this help? We got rid of the nested if expressions, and our “flatter” code is easier to follow. It’s still fugly and repetitive, though. What about head and tail ?
  • 13. Pattern matching When we construct a list, the Haskell runtime has to remember what constructors we used. It goes a step further, and makes this information available to us. We can examine the structure of a piece of data at runtime using pattern matching.
  • 14. Pattern matching on an empty list What’s the length of an empty list? myLength [ ] = 0 This is a function of one argument. If that argument matches the empty-list constructor, our function returns the value 0.
  • 15. Pattern matching on a non-empty list What’s the length of a non-empty list? myLength ( x : x s ) = 1 + myLength x s If our argument matches the non-empty-list constructor “:”, then: the head of the list is bound to the name x; the tail to xs; and the expression is returned with those bindings.
  • 16. Aaaand it’s over to you Now that we know how pattern matching works, let’s do some super-simple exercises: Write versions of the head and tail functions: head [1 ,2 ,3] == > 1 tail [ ’a’ , ’b’ , ’c ’] == > [ ’b’ , ’c ’] Give your versions different names, or you’ll have a hard time trying them out in ghci.
  • 17. Matching alternative patterns We combine our two pattern matches into one function definition by writing them one after the other: myLength [ ] = 0 myLength ( x : x s ) = 1 + myLength x s As with guards, pattern matching proceeds from top to bottom and stops at the first success. The RHS of the first pattern that succeeds is used as the body of the function.
  • 18. Matching alternative patterns We combine our two pattern matches into one function definition by writing them one after the other: myLength [ ] = 0 myLength ( x : x s ) = 1 + myLength x s As with guards, pattern matching proceeds from top to bottom and stops at the first success. The RHS of the first pattern that succeeds is used as the body of the function. Question: What do you suppose happens if no pattern matches?
  • 19. Over to you, part two And now that we know how to write function definitions that can deal with multiple patterns, another exercise: Write a version of the take function: take 3 [100 ,200 ,300 ,400 ,500] ==> [100 ,200 ,300] take 3 [ ’a’ , ’b ’] ==> [ ’a’ , ’b ’] take 3 [] ==> ???
  • 20. Over to you, part two And now that we know how to write function definitions that can deal with multiple patterns, another exercise: Write a version of the take function: take 3 [100 ,200 ,300 ,400 ,500] ==> [100 ,200 ,300] take 3 [ ’a’ , ’b ’] ==> [ ’a’ , ’b ’] take 3 [] ==> ??? Now use ghci to figure out what the drop function does, and write a version of that.
  • 21. Metasyntactic variables Languages have their cultural habits, and Haskell is no exception. You’ll very often see the names used when pattern matching a list follow a naming convention like this: (x: xs) (y: ys) (d:ds) and so on. Think of the “s” suffix as “pluralizing” a name, so “x” (ex) is the head of the list, and “xs” (exes) is the rest.
  • 22. Matching multiple patterns We can match more than one pattern at a time. Consider how we might add the elements of two vectors, represented as lists: sumVec ( x : x s ) ( y : y s ) = x + y : sumVec x s y s sumVec [ ] [] = []
  • 23. Combining pattern matching and guards Things start to get seriously expressive when we combine language features. Remember that bloated grep definition from earlier? Let’s put our new friends to work! grep n [ ] = [] grep n ( x : xs ) | i s I n f i x O f pattern x = format n x : g r e p ( n+1) x s | otherwise = g r e p ( n+1) x s
  • 24. What’s happening here? When we define a function, a pattern binds names to values. Given a list and a pattern (x: xs), if the list is non-empty, then x is bound to its head, and xs to its tail. Then each guard (if any) associated with that pattern is evaluated in turn, with those bindings in effect, until a guard succeeds. Once a guard succeeds, its RHS is used as the result, with the bindings from that pattern still in effect. If the pattern match fails, or no guard succeeds, we fall through to the next pattern and its guards.
  • 25. What’s happening here? When we define a function, a pattern binds names to values. Given a list and a pattern (x: xs), if the list is non-empty, then x is bound to its head, and xs to its tail. Then each guard (if any) associated with that pattern is evaluated in turn, with those bindings in effect, until a guard succeeds. Once a guard succeeds, its RHS is used as the result, with the bindings from that pattern still in effect. If the pattern match fails, or no guard succeeds, we fall through to the next pattern and its guards. Note: If all patterns and guards in a function definition were to fail on some input, we’d get a runtime error. That would be bad.
  • 26. And speaking of bad. . . Remember our sumVec function? sumVec ( x : x s ) ( y : y s ) = x + y : sumVec x s y s sumVec [ ] [] = [] What happens if we apply this to lists of different lengths? sumVec [1,2,3] [4,5,6,7,8]
  • 27. And speaking of bad. . . Remember our sumVec function? sumVec ( x : x s ) ( y : y s ) = x + y : sumVec x s y s sumVec [ ] [] = [] What happens if we apply this to lists of different lengths? sumVec [1,2,3] [4,5,6,7,8] So . . . what can we do about that exciting behaviour?
  • 28. One possible response Let’s declare that the sum of two vectors should end when we reach the end of the shorter vector. sumVec ( x : x s ) ( y : y s ) = x + y : sumVec x s y s sumVec what ever = [] Whoa, dude. . . Why does this work?
  • 29. One possible response Let’s declare that the sum of two vectors should end when we reach the end of the shorter vector. sumVec ( x : x s ) ( y : y s ) = x + y : sumVec x s y s sumVec what ever = [] Whoa, dude. . . Why does this work? The names “what” and “ever” are patterns. However, a plain name (with no constructors in sight) does not inspect the structure of its argument. So “what” and “ever” will each happily match either an empty or a non-empty list.
  • 30. An aside: strings are lists In Haskell, we write characters surrounded by single quotes, and strings in double quotes. Strings are lists, so: ” abc ” is syntactic sugar for [ ’ a ’ , ’b ’ , ’c ’ ] and hence for ’a ’ : ’b ’ : ’c ’ : [ ] Functions that can manipulate lists can thus manipulate strings. Oh, and escape sequences such as ”rnt” work, too.
  • 31. We are not limited to one constructor per pattern Suppose we want to squish consecutive repeats of an element in a list. compress ” f o o o b a r r r r r r ” == ” f o b a r ” > We can write a function to do this using an elegant combination of pattern matching and guards: compress ( x : y : ys ) | x == y = compress ( y : ys ) | otherwise = x : compress ( y : ys ) compress ys = ys Notice that our pattern matches on two consecutive list constructors!
  • 32. Homework Write a function that returns the nth element of a list, counting from zero. nth 2 ” squeak ” == ’ u ’ > Write a function that returns the element immdiately before the last element of a list. lastButOne [1 ,2 ,3 ,4 ,5] == 4 > Write a function that determines whether its input is a palindrome. isPalindrome ” foobar ” == F a l s e > isPalindrome ” foobarraboof ” == True >