[Haskell-beginners] infix functions with 3 args

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Fri Mar 20 11:25:30 EDT 2009


On 2009 Mar 20, at 7:05, 7stud wrote:
> func2 x y = x + y
>
> func3 x y z  = x + y + z
>
> Here are the results:
>
> *Main> func2 10 20
> 30
> *Main> 10 `func2` 20
> 30
> *Main> func3 10 20 30
> 60
> *Main> 10 `func3` 20 30
>
> <interactive>:1:11:
>    No instance for (Num (t1 -> t))
>      arising from the literal `20' at <interactive>:1:11-15


Keep in mind that any function taking multiple arguments is  
indistinguishable from a function taking a single argument and  
returning a function that takes more arguments.  So we have in this  
case "10 `func3` 20" which requires another argument. But because of  
Haskell evaluation rules, writing "10 `func3` 20 30" causes Haskell to  
try to evaluate "20 30" as a function (i.e. Haskell infers "10 `func3`  
(20 30)".  We need to tell Haskell not to do this:

	(10 `func3` 20) 30

It can easily be seen that "(10 `func3` 20)" returns a function which  
is applied to "30", as we intended.

(hm, I'm starting to sound like Oleg. not necessarily a good thing...  
this is not a research paper :)

-- 
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/beginners/attachments/20090320/e1b573c4/PGP.bin


More information about the Beginners mailing list