# Help with Closures?

I just need help understanding clojures and how to know when to use them in general, I just can't seem to wrap my head around it Well. Starting with the easy part, "Clojures" with a J is the name of a programming language, a spinoff of Scheme. The things created by a procedure call are called "closures"; it's the same root as "close" as in "close the door."

So, what's a closure? Take an algebraic expression such as ax+b. The three variables a, x, and b are free, meaning you don't know what their value is. Now consider the function f(x)=ax+b. You still don't know any of their values, but you do know that when f is called, it will provide a value for x. So in the expression f(x)=ax+b, a and b are still free, but x is now bound.

An expression with free variables is called open; an expression with no free variables is closed. The (drumroll please) closure of an expression is a containing expression that binds all the free variables, e.g., g(a,b,x)=ax+b is a closure of ax+b.

If you're doing mathematics (theory of functions), then you don't have to account for things like global variables (which are bound even when used in an expression), and so the above is the whole story. Real life is a little messier, but we don't have to worry about that now.

Okay, now I can answer the question you actually asked, namely, when would you use one. So, do you understand MAP?

I want to add 3 to each item of the list. That's what MAP does: It plugs each item of the list 7,8,1 into the empty slot in the __+3.

The "empty slot" notation is unique to Snap!. Suppose we didn't have that. We'd need an expression like . But x is a free variable in that expression, so it wouldn't work to say

(note that there's no gray ring around the x+3), because Snap! would try to evaluate x+3 and would find no binding for x -- that is, no variable x available.

So instead of x+3, we'd need the equivalent of f(x)=x+3. That's a closed expression, so we could call the function three times, with each item of the list as its input. Now we're just left with a little notational issue. We don't want to have to give the function a name, such as the f above. We need anonymous functions. In math, the notation is x↦x+3. (The symbol you don't recognize is pronounced "maps to," so "x maps to x+3" is an anonymous function.) But poor unfortunate computer programmers have to put up with keyboards instead of chalkboards, so they can't just make up whatever symbols they want, so instead they have to say
(lambda (x) (+ x 3))
which is the closure of x+3. The grey ring in Snap! is the equivalent of lambda. The exact equivalent of the lambda expression above is

The nice thing in Snap! is that unless you're doing something exotic, you don't have to think about closures at all, because the higher order functions like MAP provide the gray ring themselves, so you can just drag in the open expression and it gets closed.

I really don't know if this answers the question in your mind, but you asked a very open-ended question. If not, try refining the question.

2 Likes

Thank you, you answered my question exactly sorry for the mispelling So basically, from my understanding, a closure is just the binding of an anonymous function to a free variable?

A closure is the binding of a free variable by a call to a procedure, anonymous or otherwise. It's just especially important with anonymous functions because of the contexts in which they're typically used (higher order functions).

Ah okay so it's the process of binding a free variable, that clarifies it up a bit.
Sorry, I'm a little new to all this; especially the terminology Well, it's the result, rather than the process, but it's the result of a procedure call. Also, "a free variable" isn't quite right either; the closure of an expression is a procedure that binds all its free variables.

So it would go: Procedure Call → Closing all free variables → Closure?

Yes.