[Haskell-cafe] Re: Partial Application
Benjamin Franksen
benjamin.franksen at bessy.de
Thu Jan 18 16:29:47 EST 2007
Philippe de Rochambeau wrote:
> 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.
(This has already been said but to re-iterate:)
You can (partially apply "multiply") but not by typing "multiply 2" at the
ghci prompt. The latter is interpreted by ghci as a command to evaluate and
then /print/ the resulting value, which means it must convert it to a
textual representation, using the Show class, which is normally not
instantiated for function values.
> 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
Just import (:load) the module Text.Show.Functions which defines a Show
instance for functions.
Prelude> :m Text.Show.Functions
Prelude Text.Show.Functions> let multiply x y = x * y
Prelude Text.Show.Functions> multiply 2
<function>
Or, use let to bind the result to a variable, like
Prelude> let multiply x y = x * y
Prelude> let f = multiply 2
Prelude> f 3
6
Cheers
Ben
More information about the Haskell-Cafe
mailing list