infix functions with 3 args

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

infix functions with 3 args

7stud-2
On p. 76 of RWH, it says:

"If a function or constructor takes two or more arguments, we have
the option of using it in infix
form, where we place it between its first and second arguments."

Here is my code:

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
    Possible fix: add an instance declaration for (Num (t1 -> t))
    In the second argument of `func3', namely `20 30'
    In the expression: 10 `func3` 20 30
    In the definition of `it': it = 10 `func3` 20 30

How do you get a function to work using infix notation when it has
3 arguments?




Reply | Threaded
Open this post in threaded view
|

infix functions with 3 args

Thomas Davie

On 20 Mar 2009, at 12:05, 7stud wrote:

> On p. 76 of RWH, it says:
>
> "If a function or constructor takes two or more arguments, we have
> the option of using it in infix
> form, where we place it between its first and second arguments."
>
> Here is my code:
>
> 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
>    Possible fix: add an instance declaration for (Num (t1 -> t))
>    In the second argument of `func3', namely `20 30'
>    In the expression: 10 `func3` 20 30
>    In the definition of `it': it = 10 `func3` 20 30
>
> How do you get a function to work using infix notation when it has
> 3 arguments?

Prelude> (10 `func3` 20) 30
60

Bob
Reply | Threaded
Open this post in threaded view
|

infix functions with 3 args

Brandon S Allbery KF8NH
In reply to this post by 7stud-2
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] [hidden email]
system administrator [openafs,heimdal,too many hats] [hidden email]
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