On Dec 27, 2006, at 22:55 , michael rice wrote:

By similar reasoning the always function would seem to
have a signature

a -> (b -> a)

where the first argument is just a value and the
return value is a function that when given a possibly
different value just returns the value originally
given to always?

Is that reasoning OK? Are

a -> (b -> a) and a -> b -> a the same signature?

This is a point that has been glossed over a bit: Haskell has the notion of partial application. If you want to start with a function that takes two values, and return a function that takes one value and uses the one previously passed in, you just invoke the function with one parameter; Haskell will produce a function which takes a single argument to complete the expression. Using (*) (prefix version of multiplication) as an example:

    Prelude> :t ((*) 2)
    ((*) 2) :: (Num t) => t -> t
    Prelude> let x2 = ((*) 2) in x2 5
    10

This shows the equivalence of the type signatures (a -> a -> a) and (a -> (a -> a)), and is one of the strengths of Haskell: you can pass a section (a "partially expanded" function") wherever a function is expected.

    Prelude> map ((*) 2) [1..5]
    [2,4,6,8,10]

This doesn't only work for prefix functions, by the way; the above example is more naturally written as (2*):

    Prelude> :t (2*)
    (2*) :: (Num t) => t -> t
    Prelude> map (2*) [1..5]
    [2,4,6,8,10]

You can also say (*2), which provides the right-hand argument; this is useful for non-commutative functions like (/). But don't try it with (-), because you'll trip over an unfortunate parsing hack for negative numbers:

    Prelude> :t (-2) -- whoops, it's a number, not a function!
    (-2) :: (Num a) => a

The Prelude provides a workaround for this, though:

    Prelude> :t (subtract 2)
    (subtract 2) :: (Num t) => t -> t

--
brandon s. allbery    [linux,solaris,freebsd,perl]     [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon university    KF8NH



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to