[Haskell-cafe] Haskell purity and printing

Philip Weaver philip.weaver at gmail.com
Tue Dec 18 16:08:26 EST 2007


On Dec 18, 2007 1:00 PM, Cristian Baboi <cristian.baboi at gmail.com> wrote:

>
> This is what I "understand" so far ...
>
> Suppose we have these two values:
> a) \x->x + x
> b) \x->2 * x
> Because these to values are equal, all functions definable in Haskell must
> preserve this.
> This is why I am not allowed to define a function like
>
> h :: (a->b) -> (a->b)
> h x = x
>

Of course you can define h.  This is just a more specific (as far as types
go) version of 'id', as defined in the Prelude:

   id :: a -> a
   id x = x

where 'a' can be any type, including a function such as (a -> b).  You can
apply 'id' to either of your functions above, and get back an equivalent
function, so each of the following evaluates to 20:

    let f = id (\x -> x+x) in f 10
    let f = id (\x -> 2 * x) in f 10
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20071218/e65551b0/attachment.htm


More information about the Haskell-Cafe mailing list