Search code examples
haskellsyntaxnotation

Syntax of where block


I'm reading Programming in Haskell by Graham Hutton and it gives the following code in Chapter 13:

import Control.Applicative
import Data.Char

{- some code omitted -}

newtype Parser a = P (String -> [(a, String)])

item :: Parser Char
item = P (\ input -> case input of
                     []   -> []
                     x:xs -> [(x,xs)])

three :: Parser (Char,Char)
three = pure g <*> item <*> item <*> item
        where g a b c = (a,c)

I'm having a hard time understanding the last line

where g a b c = (a,c)

I understand that this line exists because three has type Parser(Char, Char) but what does g a b c represent? How is g a b c syntactically valid? I'm used to seeing where in cases like

f :: s -> (a,s)
f x = y
   where y = ... x ...

where each symbol x and y appear before the where declaration.


Solution

  • It is the syntax to declare a function. It is the equivalent to

     where g = \a b c -> (a,c)
    

    g is a function which takes 3 arguments and returns a tuple