[Haskell-cafe] Partial Application
Philippe de Rochambeau
phiroc at free.fr
Thu Jan 18 16:15:07 EST 2007
Hello,
my original query concerning partial application was triggered by the
following statement from Thomson's "The Craft of Functional
Programming", p. 185:
"
multiplyUC :: (Int, Int) -> Int
multiplyUC (x,y) = x * y
multiply :: Int -> Int -> Int
multiply x y = x * y
....
In the case of multiplications we can write expression like multiply 2".
When I read this, I thought that you could partially apply "multiply"
by typing "multiply 2" at the ghci prompt. However, this generated an
error:
<interactive>:1:0:
No instance for (Show (Int -> Int))
arising from use of `print' at <interactive>:1:0-9
Possible fix: add an instance declaration for (Show (Int -> Int))
In the expression: print it
In a 'do' expression: print it
After reading http://www.haskell.org/hawiki/PartialApplication, I
figured out that you can only partially apply declared functions, in
source files, not at the prompt:
multiplyBy2 = multiply 2
Now "multiplyBy2 50" yields "100"
On page 22 of "Programming in Haskell", Howard says that you can do
the following partial application of the curried function add': add'
1 :: Int -> Int, where add and add' are declared as
add :: (Int,Int) -> Int
add (x,y) = x + y
add' :: Int -> (Int -> Int)
add' x y = x + y
However, typing "add' 1" at the prompt generates an error. If I add
the following to my source file
addOne = add' 1
typing addOne 5" at the prompt yields "6", which is the right answer.
Cheers,
phiroc
More information about the Haskell-Cafe
mailing list