[Haskell-cafe] Re: Partial Application

2007-01-18 Thread Johan Grönqvist

Philippe de Rochambeau skrev:

Hello,


 > 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:


You can
Try:

let mul2 = multiply 2 in mul2 5

this should give 10 at the ghci prompt



:1:0:
No instance for (Show (Int -> Int))
  arising from use of `print' at :1:0-9
Possible fix: add an instance declaration for (Show (Int -> Int))
In the expression: print it
In a 'do' expression: print it



This error says that the result is of a type that ghci cannot print. The 
type here is Int -> Int. The object "multiply 2" is a function, and 
there is no way of printing functions built into ghci.


(Ghci can display elements of types that are instances of the Show 
typeclass, and the error complains that there is no instance Show (Int 
-> Int), i.e. no prescription for displaying functions of type Int -> Int.)




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:




Again. They can be created at the prompt, but the funcion-value can not 
be displayed as text.



multiplyBy2 = multiply 2

Now "multiplyBy2 50" yields "100"


let multiply2 = multiply 2
multiply2 50

This should give 100 at the prompt

/ johan

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


[Haskell-cafe] Re: Partial Application

2007-01-18 Thread Benjamin Franksen
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:
> 
> :1:0:
>  No instance for (Show (Int -> Int))
>arising from use of `print' at :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


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

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