The importance of first class functions for introductory languages

1.

The language I learned to program in [1] was python. I had been reading the reference documentation and knew that that functions were “objects”. I also knew a function could return another object. So I came to the conclusion that a function could return another function. (I also came to the conclusion that you could take functions as arguments).

def foo() :
    return 1

def bar() :
    return foo

2.

I had read about lexical scope after reading the reference documentation, if I defined a variable outside of the functions, I could use it inside (with some arbitrary restrictions python gives)

a = 4
def foo() :
    return a 

3.

I guessed that I could define a function inside another function. Initially I did this to keep very specific functions outside of code. It came naturally to me because it didn't feel like anything was special about functions in python, you could nest other block structures so I should be able to nest functions.

def foo(a) :
    def bar(x) : 
        return x+2
    return bar(a)

4.

I realized that I could use lexical scope to make custom functions. I was pretty sure this wouldn't work, but I decided to try it anyways, It seemed like witchcraft. It ended up working just fine.

def foo(a)  :
    def bar() :
        return a
    return map(bar,range(100))

5. (final)

I realized that I could return the custom functions I constructed and use them elsewhere.

def foo(a) : 
    def bar(): 
        return a
    return bar

I was stoked when I figured this out, I could create custom functions by calling other functions it was amazing. I called this “invention” a “function factory”, a function that returned another function.

The moral of the story.

If you are familiar with computer science, you know this term by a different name. The correct term for such a construct is a “lexical closure”. I had never even read the term “lexical closure” before, yet I was able to stumble upon it because the language didn't treat functions as something special. There was never special syntax for doing lexical closures, they were something you could accidentally stumble upon. When functions are special, you stop asking yourself about what you can do with them, and simply think that you can't do anything with them other than what you already know. If C was my first language, my thought patterns would have ossified and I would have never thought about defining a function in another function for any language. I would have needed to read about lexical closures before could even think about doing them. When I learned C, I actually tried doing a lexical closure, and was disappointed I couldn't do it.

I don't think I could have stumbled upon (IE not read it in a book) lexical closures if Common Lisp was my first language either, although I might have. You have to jump through hoops to use functions as parameters, but you can still do it, It's just harder to figure out because functions reside in their own namespace.

notes

  1. I had used other programming languages before, such as perl and C#, but I didn't learn to program in them. I never learned deeply about them, or understood the art of programming from them.

By Logan Andersen. This work is licensed under CC BY-ND 4.0

You can see my other blogs here.