[Haskell-cafe] Re: Pattern matching does not work like this?
Maurício
briqueabraque at yahoo.com
Wed Jul 15 10:09:40 EDT 2009
> I do not notice this before. "fun ([0, 1] ++ xs) = .." in my code
> could not be compiled, parse error.
Maybe a small abstract can help, as I once also got
confused by that.
* First, syntax without operators
You can only match on constructors. So, if
you have
data Test = Test1 String | Test2 Integer | Test3
you can do
function (Test1 s) = ...
function (Test2 i) = ...
function Test3 = ...
* Second, syntax with operators
Haskell allow constructors made of symbols, but you
have to start them with ':', so this is valid:
data Test = Test1 String | Integer :** String
and then
function (Test1 s) = ...
function (i :** s) = ...
* Third, special syntax
Haskell has special syntax for tuples and lists (and
something else I forgot?). You can ask information
about a name in ghci using ':i <name>', see what it
says about (,) and []:
data (,) a b = (,) a b
data [] a = [] | a : [a]
As you can see, (,), [] and : are actually constructors,
and you can pattern match on them:
function [] = ...
function (a:b) = ...
function ((:) a b) = ...
function (a,b) = ...
function ((,) a b) = ...
Best,
Maurício
More information about the Haskell-Cafe
mailing list