Pattern Matching

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Pattern Matching

caseyh
Why in a pattern match like

score (1 3) = 7

can I not have

sizeMax = 3

score (1 sizeMax) = 7

--
Regards,
Casey
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching

Brandon S Allbery KF8NH
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] [hidden email]
system administrator [openafs,heimdal,too many hats] [hidden email]
electrical and computer engineering, carnegie mellon university    KF8NH



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

PGP.sig (202 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching

John Dorsey-2
In reply to this post by caseyh
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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching

Andrew Coppin
In reply to this post by caseyh
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... ;-)

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching

Colin Paul Adams
>>>>> "Andrew" == Andrew Coppin <[hidden email]> 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching

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...)

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching

Malcolm Wallace
> (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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching

Daniel Fischer-4
In reply to this post by Andrew Coppin
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?



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching

caseyh
In reply to this post by caseyh
Thank you to all who replied, very instructive.
--
Regards,
Casey
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe