13 Nov 03:15
13 Nov 03:22
Re: Pattern Matching
Brandon S. Allbery KF8NH <allbery <at> ece.cmu.edu>
2009-11-13 02:22:42 GMT
2009-11-13 02:22:42 GMT
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
13 Nov 03:30
Re: Pattern Matching
John Dorsey <haskell <at> colquitt.org>
2009-11-13 02:30:38 GMT
2009-11-13 02:30:38 GMT
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
13 Nov 10:55
Re: Pattern Matching
Andrew Coppin <andrewcoppin <at> btinternet.com>
2009-11-13 09:55:41 GMT
2009-11-13 09:55:41 GMT
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...![]()
13 Nov 11:00
Re: Pattern Matching
Colin Paul Adams <colin <at> colina.demon.co.uk>
2009-11-13 10:00:00 GMT
2009-11-13 10:00:00 GMT
>>>>> "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
13 Nov 11:05
Re: Pattern Matching
Andrew Coppin <andrewcoppin <at> btinternet.com>
2009-11-13 10:05:15 GMT
2009-11-13 10:05:15 GMT
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...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...)> > So why aren't they? >
13 Nov 11:31
Re: Pattern Matching
Malcolm Wallace <malcolm.wallace <at> cs.york.ac.uk>
2009-11-13 10:31:35 GMT
2009-11-13 10:31:35 GMT
> (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
13 Nov 13:02
Re: Pattern Matching
Daniel Fischer <daniel.is.fischer <at> web.de>
2009-11-13 12:02:53 GMT
2009-11-13 12:02:53 GMT
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...It yells "overlapping patterns" -- you do pass -Wall, don't you?> > > > 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...)
RSS Feed