[Haskell-beginners] Looking for more information about Pattern Matching

Daniel Fischer daniel.is.fischer at googlemail.com
Fri Apr 29 20:00:38 CEST 2011


On Friday 29 April 2011 18:43:51, Mike Meyer wrote:
> On Fri, 29 Apr 2011 03:27:48 +0200
> 
> Daniel Fischer <daniel.is.fischer at googlemail.com> wrote:
> > On Friday 29 April 2011 02:35:13, Graham Hopper wrote:
> > Basically, a pattern is
> 
> [...]
> 
> > Or
> > 
> > - a constructor application, (Con arg1 arg2), (x:xs), (a,b,c,d), ((:)
> > x xs)
> 
> [...]
> 
> > Also there are
> 
> [...]
> 
> I've been wondering about this case. Since : is "just another
> function",

It's "just another function" in the same sense as Just or Right are "just 
another function".

They all are (non-nullary) constructors.
They are "just another function" in the sense that they take arguments and 
produce values, and you can pass them to any higher order function like 
every other function (that is, if the type matches) or have them as results 
of other functions.

They are however special functions in the sense that, as they are 
constructors, you can pattern-match on them (fully applied, you can't do

foo :: (Char -> Either Char Int) -> Bool
foo Left = True
foo _ = False
).

> then if things were consistent, I ought to be able to use
> other functions in constructor applications. But my attempts to do so
> always seem to result in syntax errors.
> 
> So my question is - what are the rules for constructor applications?
> Is the list above complete?

I think this time I haven't forgotten anything, but don't be too sure, 
better check the report.

> Are there just a few more? Is there some
> rule to determine which functions are ok in patterns and which aren't?

You can match on constructor applications, nothing else, except for the few 
special cases,

- number literals (sugar for an equality test)
- character literals ('a' is not really a constructor, matching on 
character literals also gives rise to an equality test)

- string literals ("foo")
- list patterns ([x1,x2,x3])

Special syntax for convenience, reduces to ('f' : 'o' : 'o' : []) resp. (x1 
: x2 : x3 : []).

- tuples are constructor applications, but with special (wired-in) syntax, 
so those patterns don't match the normal cases of

Con pat1 pat2 ... patN  -- prefix constructor application
pat1 :$%& pat2    -- infix constructor application

- labelled patterns, Con{ field1 = pat1, ..., fieldN = patN } (N >= 0)

You can tell constructors apart from "ordinary functions" by case, if the 
name begins with an upper-case letter, it's a constructor, if it begins 
with a lower-case letter or an underscore, it's an "ordinary function".

For operators, the rule is the same if you regard ':' as an upper-case-
symbol (and the only one); an infix operator is a constructor if and only 
if it starts with ':'.

So, (x : xs), infix application of the operator (:), : obviously starts 
with ':', hence a constructor ~> valid pattern.
(x && y); infix application of the operator (&&), && doesn't start with 
':', no constructor ~> not a valid pattern.

> 
>      Thanks,
>      <mike



More information about the Beginners mailing list