schmauss
schmauss

Reputation: 53

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.

Upvotes: 2

Views: 127

Answers (2)

mb14
mb14

Reputation: 22596

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

Upvotes: 9

Alexey Romanov
Alexey Romanov

Reputation: 170713

How is g a b c syntactically valid?

It's valid for the same reason same definition on the top-level of the module would be valid. The difference between definitions in where and top level is just that you have variables bound in function's head (e.g. x in your last example) in scope and can use them on the right side, but this doesn't mean you have to use them.

Upvotes: 2

Related Questions