[Haskell-beginners] Explanation of double astrix

Bayley, Alistair Alistair.Bayley at invesco.com
Wed Sep 3 11:09:45 EDT 2008


> From: beginners-bounces at haskell.org 
> [mailto:beginners-bounces at haskell.org] On Behalf Of Paul Johnston
> 
> 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]
> 
> Curious as to what is going on.
> *Main> :t (\x -> 2 ** x)
> (\x -> 2 ** x) :: (Floating t) => t -> t
> *Main> :t (\x -> 2 * x)
> (\x -> 2 * x) :: (Num t) => t -> t
> 
> 
> Somehow the type has gone from Num to Floating
> I am using the excellent (IMHO) tutorial by Hal Daume and the 
> book by Graham 
> Hutton but can find no clues.
> 
> Cheers Paul


http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#
v%3A**

(**) is a function in the Floating class (Double and Float are instances
of this class).

(*) is a function in the Num class.

So, replacing (*) with (**) has changed the (Num t) constraint to a
(Floating t) constraint. Note that the type of your lambda is still t ->
t; it's just the class constraint which has changed (because (**) comes
from a different class than (*) ).

(The class hierarchy for Floating is: Num => Fractional => Floating, so
Floating has all of the methods of Fractional, and therefore Num).

Alistair

*****************************************************************
Confidentiality Note: The information contained in this message,
and any attachments, may contain confidential and/or privileged
material. It is intended solely for the person(s) or entity to
which it is addressed. Any review, retransmission, dissemination,
or taking of any action in reliance upon this information by
persons or entities other than the intended recipient(s) is
prohibited. If you received this in error, please contact the
sender and delete the material from any computer.
*****************************************************************



More information about the Beginners mailing list