simple task: all I want is a function to apply a list of parameters to a curried function.
Let's say our function is the famous add
one:
fun add a b = a + b;
Now all I want is a function to apply a list (say [1, 5]
) to add
. This would then look like:
apply add [1, 5];
This seems to be harder than I thought. My try:
fun apply f ps = foldl (fn (p, f') => f' p) f ps;
But this gets this hilarious readable error message:
Error: operator and operand don't agree [circularity]
operator domain: 'Z * ('Z -> 'Y) -> 'Z -> 'Y
operand: 'Z * ('Z -> 'Y) -> 'Y
in expression:
foldl (fn (p,f') => f' p)
Now, what is wrong about my implementation? Is it even possible in SML/NJ?
Cheers and thanks for hints and answers.
The root of the problem is that for this to work, the type of the folded function would have to change as it's folded through the list, and this (hopefully obviously) isn't possible.
The "circularity" in (fn (p, f') => f' p)
comes from the fact that the type of f' p
must be the same as the type of f'
.
My personal intuition is that what you're attempting can't be possible, because apply add []
, apply add [1]
, and apply add [1,2]
would have to have different types.
And apply add [1,2,3]
doesn't make much sense at all.
A useful exercise might be to attempt to write down the type of apply
.