[Haskell-beginners] Explanation of double astrix

Chaddaï Fouché chaddai.fouche at gmail.com
Wed Sep 3 11:12:41 EDT 2008


2008/9/3 Paul Johnston <Paul.A.Johnston at manchester.ac.uk>:
> Hi
> Was playing around with ghci and lambda expressions and:
>
> *Main> map (\x -> 2 * x) [1 ..3]
> [2,4,6]
>
> Then thinking back to Fortran (yes I'm not young anymore!)
>
> *Main> map (\x -> 2 ** x) [1 ..3]
> [2.0,4.0,8.0]

I don't know what (**) did in Fortran, but in Haskell it's a
exponentiation, which is why it only works on floating point numbers
(Double and Float are two instances of the Floating typeclass).

The definition of (**) in the Prelude :
x ** y              =  exp (log x * y)

You can quickly find it using Hoogle (I recommend you take the habit
of using this excellent tool).

For integral powers, you can use (^).

-- 
Jedaï


More information about the Beginners mailing list