Calling "zipWith" using "^" function.

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

Calling "zipWith" using "^" function.

Venu Chakravorty
Hello there,
This is just some toy program I was trying. I fail to understand why this works:

=============================
Prelude> zipWith (\ x y -> ((x ^ y) / (product [1..x]))) [1..3] [2,2,2]
[1.0,2.0,1.5]
=============================

and this does not:

=============================
Prelude> zipWith (\ x y -> ((y ^ x) / (product [1..x]))) [1..3] [2,2,2]

<interactive>:1:19:
    Ambiguous type variable `a' in the constraints:
      `Fractional a' arising from a use of `/' at <interactive>:1:19-44
      `Integral a' arising from a use of `^' at <interactive>:1:20-24
    Probable fix: add a type signature that fixes these type variable(s)
============================
Note that the "^" function has "x" and "y" flipped.

:t (^) says:
============================
(^) :: (Num a, Integral b) => a -> b -> a
============================

Could somebody please throw some light?

Thanks.

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Calling "zipWith" using "^" function.

yi lu
Prelude> :t (**)
(**) :: Floating a => a -> a -> a

On Mon, Nov 9, 2015 at 12:14 PM, Venu Chakravorty <[hidden email]> wrote:
Hello there,
This is just some toy program I was trying. I fail to understand why this works:

=============================
Prelude> zipWith (\ x y -> ((x ^ y) / (product [1..x]))) [1..3] [2,2,2]
[1.0,2.0,1.5]
=============================

and this does not:

=============================
Prelude> zipWith (\ x y -> ((y ^ x) / (product [1..x]))) [1..3] [2,2,2]

<interactive>:1:19:
    Ambiguous type variable `a' in the constraints:
      `Fractional a' arising from a use of `/' at <interactive>:1:19-44
      `Integral a' arising from a use of `^' at <interactive>:1:20-24
    Probable fix: add a type signature that fixes these type variable(s)
============================
Note that the "^" function has "x" and "y" flipped.

:t (^) says:
============================
(^) :: (Num a, Integral b) => a -> b -> a
============================

Could somebody please throw some light?

Thanks.

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Calling "zipWith" using "^" function.

Max Voit-2
In reply to this post by Venu Chakravorty
On Sun, 8 Nov 2015 23:14:11 -0500
Venu Chakravorty <[hidden email]> wrote:

> <interactive>:1:19:
>     Ambiguous type variable `a' in the constraints:
>       `Fractional a' arising from a use of `/' at
> <interactive>:1:19-44 `Integral a' arising from a use of `^' at
> <interactive>:1:20-24 Probable fix: add a type signature that fixes
> these type variable(s) ============================
 
> Could somebody please throw some light?

Consider the types of both (^) and (/). Then take a look at

  https://www.haskell.org/onlinereport/haskell2010/haskell2x.png

and compare with your error message. Note especially that there is no
connection from Integral to Fractional or vice versa.

As a solution use either the already suggested (**) or fromIntegral
(mind the last subsection of
https://www.haskell.org/onlinereport/haskell2010/haskellch6.html )

best,
Max
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners