Shouldn’t this definition be allowed in a lazy language like Haskell in which functions are curried?
apply f [] = f
apply f (x:xs) = apply (f x) xs
It’s basically a function that applies the given function to the given list of arguments and is very easily done in Lisp for example.
Are there any workarounds?
It is hard to give a static type to the
applyfunction, since its type depends on the type of the (possibly heterogeneous) list argument. There are at leasttwo waysone way to write this function in Haskell that I can think of:Using reflection
We can defer type checking of the application until runtime:
Note that now the Haskell program may fail with a type error at runtime.
Via type class recursion
Using the semi-standard
Text.Printftrick (invented by augustss, IIRC), a solution can be coded up in this style (exercise). It may not be very useful though, and still requires some trick to hide the types in the list.Edit: I couldn’t come up with a way to write this, without using dynamic types or hlists/existentials. Would love to see an example