Casey Hawthorne | 13 Nov 03:15
Picon

Pattern Matching

Why in a pattern match like

score (1 3) = 7

can I not have

sizeMax = 3

score (1 sizeMax) = 7

--
Regards,
Casey
Picon
Favicon

Re: Pattern Matching

On Nov 12, 2009, at 21:15 , Casey Hawthorne wrote:
> Why in a pattern match like
>
> score (1 3) = 7
>
> can I not have
>
> sizeMax = 3
>
> score (1 sizeMax) = 7

Because it's a pattern, and when you introduce a symbol you are  
inviting the pattern match to bind what it matched to that name for  
use within the function.  (Ordinary arguments are a trivial case of  
this.)

Or, by example:

 > score (1 sizeMax) = (expression using sizeMax)

The normal way to do what you want is guards:

 > score (1 x) | x == sizeMax = 7 -- you can pronounce the "|" as  
"such that"

--

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery <at> kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery <at> ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
John Dorsey | 13 Nov 03:30

Re: Pattern Matching

Casey,

> Why in a pattern match like
> 
> score (1 3) = 7

You probably mean
> score 1 3 = 7

which applies the function 'score' to two arguments.  With the parentheses,
it looks like an application of '1' to the argument '3'.  But to answer your
actual question...

> can I not have
> 
> sizeMax = 3
> 
> score (1 sizeMax) = 7

When a variable name (such as 'sizeMax') appears in a pattern, it gets bound
there.  This is useful so you can refer to the variable on the right hand
side, as in:

successor x = x + 1

How would the compiler know whether to bind the variable (what actually
happens), or match against the value represented by some earlier binding
(what you're asking for)?

What if the type of that second argument doesn't have a defined equality
operation?

There might be some reasonable way to do it, but I suspect it would be
fragile and error-prone, at best.  So it's always a new binding.  It's easy
enough to work around:

score 1 x | x == sizeMax = 7

Regards,
John
Andrew Coppin | 13 Nov 10:55

Re: Pattern Matching


Casey Hawthorne wrote: > Why in a pattern match like > > score (1 3) = 7 > > can I not have > > sizeMax = 3 > > score (1 sizeMax) = 7 >
If I had a dollar for every time I've written something like case msg of eVENT_QUIT -> ... eVENT_POST -> ... eVENT_RESIZE -> ... and spent an hour trying to figure out why the messages aren't being processed right... ;-)
Colin Paul Adams | 13 Nov 11:00
Picon
Picon
Favicon

Re: Pattern Matching

>>>>> "Andrew" == Andrew Coppin <andrewcoppin <at> btinternet.com> writes:

    Andrew> Casey Hawthorne wrote:
    >> Why in a pattern match like
    >> 
    >> score (1 3) = 7
    >> 
    >> can I not have
    >> 
    >> sizeMax = 3
    >> 
    >> score (1 sizeMax) = 7
    >> 

If I had a dollar for every time I've written something like

    Andrew>  case msg of eVENT_QUIT -> ...  eVENT_POST -> ...
    Andrew> eVENT_RESIZE -> ...

    Andrew> and spent an hour trying to figure out why the messages
    Andrew> aren't being processed right... ;-)

So why aren't they?

--

-- 
Colin Adams
Preston Lancashire
Andrew Coppin | 13 Nov 11:05

Re: Pattern Matching


Colin Paul Adams wrote: > If I had a dollar for every time I've written something like > > Andrew> case msg of eVENT_QUIT -> ... eVENT_POST -> ... > Andrew> eVENT_RESIZE -> ... > > Andrew> and spent an hour trying to figure out why the messages > Andrew> aren't being processed right... ;-) > > So why aren't they? >
Because what I *should* have written is case msg of _ | msg == eVENT_QUIT -> ... | msg == eVENT_POST -> ... | msg == eVENT_RESIZE -> ... which is something quite different. (And, entertainingly, because the incorrect version is perfectly valid source code, no compiler errors or warnings...)
Malcolm Wallace | 13 Nov 11:31
Picon

Re: Pattern Matching


> (And, entertainingly, because the incorrect version is perfectly > valid source code, no compiler errors or warnings...)
If you actually turn on compiler warnings (-Wall), I think you will see something like andrew.hs:10:10: Warning: This binding for `eVENT_QUIT' shadows the existing binding defined at EventLog.hs:43:0 In a case alternative and so forth, for every incorrect alternative. Regards, Malcolm
Daniel Fischer | 13 Nov 13:02
Picon

Re: Pattern Matching

Am Freitag 13 November 2009 11:05:15 schrieb Andrew Coppin:

> Colin Paul Adams wrote: > > If I had a dollar for every time I've written something like > > > > Andrew> case msg of eVENT_QUIT -> ... eVENT_POST -> ... > > Andrew> eVENT_RESIZE -> ... > > > > Andrew> and spent an hour trying to figure out why the messages > > Andrew> aren't being processed right... ;-) > > > > So why aren't they? > > Because what I *should* have written is > > case msg of > _ | msg == eVENT_QUIT -> ... > > | msg == eVENT_POST -> ... > | msg == eVENT_RESIZE -> ... > > which is something quite different. > > (And, entertainingly, because the incorrect version is perfectly valid > source code, no compiler errors or warnings...)
It yells "overlapping patterns" -- you do pass -Wall, don't you?
Casey Hawthorne | 14 Nov 00:12
Picon

Re: Pattern Matching

Thank you to all who replied, very instructive.
--
Regards,
Casey

Gmane