How do I define Lisp’s apply in Haskell?

currying, haskell, type-inference, types, variadic-functions

Solution

It is hard to give a static type to the `apply` function, since its type depends on the type of the (possibly heterogeneous) list argument. There are at least two ways one way to write this function in Haskell that I can think of:

Using reflection

We can defer type checking of the application until runtime:

import Data.Dynamic
import Data.Typeable

apply :: Dynamic -> [Dynamic] -> Dynamic
apply f []      = f
apply f (x:xs)  = apply (f `dynApp` x) xs

Note that now the Haskell program may fail with a type error at runtime.

Via type class recursion

Using the semi-standard `Text.Printf` trick (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

Problem

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?

Original source

Related problems