| add inc1 inc2 |

add := [:a :b :c | a + b + c] curried.

inc1 := add value: 1.

inc2 := inc value: 2.

(inc2 value: 2) + (inc2 value: 3)

Would it make sense to change Currier like:

value: anObject

block numArgs = 1

ifTrue: [^block value: anObject].

block numArgs > 1

ifTrue: [^self class new block: (CurriedBlock new block: block arguments: (Array with: anObject))] .

self error: ‘Incorrect number of arguments’

and implement the missing

valueWithArguments: anArray

^block valueWithPossibleArguments: anArray

and

valueWithPossibleArguments: anArray

^block valueWithPossibleArguments: anArray

accordingly?

Did you somehow use curried blocks somewhere? What was your use case?

]]>df := [:h :f :x | (f value: x + h) – (f value: x) / h].

dnf := [:h :n :f :x | n = 0

ifTrue: [ f value: x ]

ifFalse: [dnf value: h

value: n-1

value: (df value: h value: f) ]

].

snddf := [:h :f | dnf value: h value: 2 value: f ]

snddf1 := [:h :f :x | dnf value: h value: 2 value: f value: x ]

snddf2 := [:h | dnf value: h value: 2 ]

But in smalltalk, even with the curried blocks, as I undertand it, the 3 flavors of snddf differ in the way of calling them as you may not be able to pass more arguments that the snddf block expects.

i.e.

(snddf value: h value:f) value: x.

snddf1 value: h value: f value: x.

(snddf2 value: h) value: f value: x.

This is not true for haskell as doing

snddf h f x

snddf’ h f x

snddf” h f x

will produce the same result.

So, unless you also override value:value:value: so that the remaining arguments are sended to the result of evaluating the block with less arguments (which I think is also doable) it won’t be exactly the same.

The only problem I find for doing such a thing is that in systems where blocks are processed as VM primitives, you will need to hack into the VM to modify the value:value:value: behavior, or remove the primitive, to end up with a not-at-all performant solution.

df := [:h :f :x | (f value: x + h) – (f value: x) / h]

dnf := [:h :n :f |

(n equals: 0)

ifTrue: [ ^ f ]

ifFalse: [ ^ dnf value: h value: (n-1) value: (df value: h value: f) ]

]

or (as I do not understand the difference between the two in Smalltalk as I have said previously, I am not sure which one is correct — or both), perhaps it is

df := [:h :f :x | (f value: x + h) – (f value: x) / h]

dnf := [:h :n :f :x |

(n equals: 0)

ifTrue: [ ^ f value: x ]

ifFalse:

[ ^ (dnf value: h value: (n-1) value: (df value: h value: f)) value: x ]

]

Still needing help with my Smalltalk… :D

]]>df := [:h :f :x | (f value: x + h) – (f value: x) / h]

dnf := [:h :n :f :x |

(n equals: 0)

ifTrue: [ ^ f value: x ]

ifFalse: [ ^ dnf value: h value: (n-1) value: (df value: h value: f) ]

]

(It is also bothers me that I don’t know how to put indented code in this posts… Vasili, how did you do it in your post? Thanks! ;))

Then you can have

snddf := [:h :f | dnf value: h value: 2 value: f ]

I am not sure about the relation of this other two versions using Smalltalk (in Haskell they would have been totally equivalent to the first one:

snddf’ := [:h :f :x | dnf value: h value: 2 value: f value: x ]

snddf” := [:h | dnf value: h value: 2 ]

Some Smalltalker can help me? ;)

Thanks!!

df := [:f :x :h | (f value: x + h) – (f value: x) / h]

but as I am not an Smalltalker, I cannot write my examples as blocks… :D However, note that the previous code is equivalent to the equation

derive f x = (f x + h – f x) / h

for a fixed (very small) value of h (this notation is Haskell, where curried functions are the thing… :))

(Another thing to note is that “derive” is only an approximation to the real derivation function by means of Newton’s formula).

Now, if you want to define the n-th derivation of a function, you usually use the following equations:

deriveN 0 f = f

deriveN n f = deriveN (n-1) (derive f)

But the last use of derive has the “wrong” number of arguments!!

Indeed it is not wrong, because (deriveN (n-1)) is expecting a function from numbers to numbers: the derivation of f!! And that is exactly the answer that (derive f) produces!

This is taken from my lectures on Functional Programming for the Universities of La Plata and Quilmes, in Argentina.

My attempt (please correct me if I made any mistakes!!) for the deriveN function as a block is

df := [:f :x | (f value: x + h) – (f value: x) / h]

dnf := [:n :f :x |

(n equals: 0)

ifTrue: [ ^ f value: x ]

ifFalse: [ ^ dnf value: (n-1) value: (df value: f) ]

]

Note that I am using df as a curried function, because the second argument to dnf has to be a function (block) with one argument!

Also note that is it important that h takes its value in some other way, not by putting it as an argument of dnf…

I hope that this shows some usefullnes…

Fidel.