[Haskell-cafe] Pattern Matching

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Thu Nov 12 21:22:42 EST 2009


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20091112/735322ad/PGP.bin


More information about the Haskell-Cafe mailing list