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

Daniel Fischer daniel.is.fischer at googlemail.com
Fri Apr 29 03:27:48 CEST 2011


On Friday 29 April 2011 02:35:13, Graham Hopper wrote:
> Hi,
> 
> I am very much a newbie to Haskell and currently working through
> http://learnyouahaskell.com and trying some of the
> http://www.haskell.org/haskellwiki/99_questions. I am struggling to get
> my head around pattern matching. I think I can understand the
> individual patterns presented in LYAH, but can't seem to find any
> information about generalising the formats.
> 
> Is there a good resource that describes the patterns that are available
> or provides more details about how to create them.
> 
> For example the format of [x] and (x:xs) seem very different and I cant
> quite get a handle on the syntax(?) behind them.

The second is an ordinary constructor-application-pattern, the first is not 
a true pattern, it's syntax sugar for lists.

Basically, a pattern is

- a wildcard, '_'
- a variable pattern, var

these two match everything, a wildcard does no binding, a variable pattern 
binds the matched value to the name and makes it available for use (on the 
rhs of the definition, after the '->' in a case expression, ...)
Or

- a constructor application, (Con arg1 arg2), (x:xs), (a,b,c,d), ((:) x xs)

the constructor has to be saturated, so if it takes n arguments to 
construct a value of type t, it has to be supplied with n arguments in a 
pattern-match. These arguments are themselves patterns (variable patterns 
in the example), so patterns can be nested (to arbitrary depth, where 
arbitrary means limited by the available memory).

Also there are 

- as-patterns, var@(a1:a2:rest), which matches against the detailed pattern 
and binds the entire value to var and the components to the corresponding 
names in the as-pattern

- lazy patterns, ~(x:xs), which do no matching immediately and only try to 
match and bind when a component is demanded (that can lead to run-time 
errors if e.g. the value is in fact an empty list).

Then there are some special cases which are not really patterns but quasi-
patterns, supplied for convenience:

- number literals, 0, 1, 1.245, ...
foo 1 = bar
is in fact
foo n | n == 1 = bar

- record patterns, Con{ field = value }, Con{ field1 = value1, field2 = 
value2 }
those are shorthand for (Con _ _ value _ _) etc. with the appropriate 
number of wildcards and the value-pattern in the correct place
One can use the special form of no supplied values, Con{}, even for 
datatypes declared without record-syntax.

- special forms of list patterns, "string", [a,b], which are desugared to
('s':'t':'r':'i':'n':'g':[]) resp. (a:b:[])

- tuple patterns, (a,b), (a,b,c,d,e)
these are strictly ((,) a b), ((,,,,) a b c d e)


> I can see how patterns
> work when shown an example, but wouldn't know how to create one from
> scratch.
> 
> I hope you can understand what I am trying to say - its a little
> difficult to explain.
> 
> 
> Thanks
> 
> Graham



More information about the Beginners mailing list