Take the 2-minute tour ×

To me a Closure is a (nested?) function with co-located data.

When you write software in Haskell and look it through afterwards, you frequently find closures that you have created unintentionally.

I do not quite get this right for myself. In what situations would I intentionally want to code closures? After all, in all examples I find the amount of co-located data is trivial/small and thus it does not quite seem to me as if in practice that would ever justify their (intentional) creation. Is there any Haskell module that would support me in intentionally creating closures and e.g. storing them in a map?

share|improve this question
Interesting question… What exactly do you mean by unintentionally created closures? –  Jonathan Sterling Jan 31 '12 at 23:17
@Jonathan Sterling mostly when you create nested functions. Usually they fulfill the criteria to be called a "closure" –  J Fritsch Jan 31 '12 at 23:52
as dflemstr points out you're creating "closures" all the time. This isn't even a concept worth having a name in haskell. Do you understand currying and partial application, and how the type signature Int -> Int -> Int is equivalent to Int -> (Int -> Int)? –  jberryman Feb 1 '12 at 18:29
@jberryman That the concept isn't even worth mentioning is only an opinion of course. –  J Fritsch Feb 1 '12 at 21:08

2 Answers 2

up vote 14 down vote accepted

In Haskell, functions are an essential part of the language, largely because Haskell is based on the Lambda Calculus.

In the Lambda Calculus, there are functions that have "free variables", meaning that they use variables that were not passed as direct parameters to them. Functions with free variables are what you would call "closures" in this case.

Because functions with free variables are so common in LC, they also form an integral part of the Haskell language. For example, when you write this:

f a b c = a * b + c

... you could also be writing this, with the exact same result:

f a b = \ c -> a * b + c

... or even:

f a b = let product = a * b in \ c -> product + c

... further equivalent changes:

f a = \ b -> let product = a * b in \ c -> product + c

This is because Haskell essentially creates functions with free variables everywhere, and thus, closures are created all the time. Some of these might be optimized away by the compiler, but it is safe to assume that there will be more closures used than you might ever be able to discover on your own.

So, don't try to locate closures; they are nothing special in Haskell and are used all the time.

share|improve this answer
All understood. Still what is not clear to me: Can I in Haskell (a) intentionally create closures and (b) store them in a map and (c) serialize them without fuss just as I could do in Erlang with term_to_binary()? –  J Fritsch Feb 1 '12 at 15:33
@JFritsch, you can easily store functions in a map, and about "intentionally creating closures"; Haskell has no distinction between closures and normal functions, so it's as easy as just storing normal functions in a map. It is very difficult to serialize functions, however, and while there's some experimental support in GHC for it, there's no reliable way of doing it yet. Recall that variables can refer to e.g. file handles, and if a closure relies on such a variable, you can't really convert it into a bit stream. –  dflemstr Feb 1 '12 at 15:48
So I won't be able to serialize my resulting map with Data.Binary? –  J Fritsch Feb 1 '12 at 16:08
No, for a map to be serializable, the keys and the values need to be serializable. Strings are serializable, arbitrary functions are not. –  dflemstr Feb 1 '12 at 16:53
@The_Ghost they are not variables in the C sense, but they are variables in the mathematical sense. Meaning, they cannot be "modified," but they can vary between function calls, i.e. the value a variable has can be different every time a function is called. –  dflemstr Apr 25 at 15:40

Typically, if I need to precompute tables or something for a function, or there's a function that requires a lot of data, yeah, that'll be done as a closure. For example, on my AI homework the other day, I wrote a thing that learned from a lot of samples and then popped out a (Point -> Bool) function, but that function had to depend on a lot of data that I'd accumulated from the learning process.

share|improve this answer
Can you pls explain that a bit more or post some sample code? –  J Fritsch Feb 4 '12 at 9:27

Your Answer


