[Haskell-cafe] Partially applied functions

Steffen Schuldenzucker sschuldenzucker at uni-bonn.de
Sat Nov 28 11:02:53 EST 2009


Ozgur Akgun wrote:
> Hi cafe,
> 
> Is such a thing possible,
> 
> 
> add :: Int -> Int -> Int
> add x y = x + y
> 
> -- a list of partially applied functions
> adds = [add 3, add 5, add 7, add 3, add 5, add 8]
> 
> -- an example usage of the list
> k = map (\ f -> f 10 ) adds
> 
> add3s = filter (?) adds -- add3s = [add 3, add 3]
> addEvens = filter (?) adds --addEvens = [add 8]
> 
> 
> I want to have functions in place of the ? signs. I guess one would need
> a way of extracting the applied value from a partially applied function
> (or totally, doesn't matter)

Well, sure you can:

add3s = filter (\f -> f 0 == 3) adds
addEvens = filter (\f -> isEven $ f 0) adds

This is only possible since there is that special property of the
addition that (add a) 0 == a forall a, i.e. you can extract the first
parameter back out of the partial applied function by passing 0 as a
second parameter.

It clearly depends on the function how much information about the
parameters can be read from the result.


-- Steffen



More information about the Haskell-Cafe mailing list