
Why in a pattern match like score (1 3) = 7 can I not have sizeMax = 3 score (1 sizeMax) = 7 -- Regards, Casey

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@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

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

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

"Andrew" == Andrew Coppin
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

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

(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

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?
participants (7)
-
Andrew Coppin
-
Brandon S. Allbery KF8NH
-
Casey Hawthorne
-
Colin Paul Adams
-
Daniel Fischer
-
John Dorsey
-
Malcolm Wallace