The little programming language of [%x interp %] isn’t extensible
So add a way to define and call functions
And then look at design tactics this opens up
[%inc example_def.py mark=python %]
[%inc example_def.py mark=def %]
"func"A function is just another object
Assign it to a variable so we can call it later
[%inc example_def.py mark=save %]
An [%g anonymous_function “anonymous function” %] is one that doesn’t have a name
JavaScript and other languages use them frequently
Python supports limited [%g lambda_expression “lambda expressions” %]
[%inc example_def.py mark=lambda %]
[%inc example_def.py mark=call %]
Evaluate arguments.
Look up the function.
Create a new environment.
Call do to run the function’s action and captures
the result.
Discard environment created in step 3.
Return the result.
Python and most other languages (including ours) are eager
R is lazy
A design choice
A variable x in a function shouldn’t clobber a
variable with the same name in its caller
Use a list of dictionaries to implement a [%g call_stack “call stack” %]
Each dictionary called a [%g stack_frame “stack frame” %]
Look down the stack to find the name
If not found, add to the current (top-most) frame
[%inc func.py mark=func %]
[%inc func.py mark=call %]
[%inc func.tll %] [%inc func.out %]
Searching active stack for a variable is called [%g dynamic_scoping “dynamic scoping” %]
Have to trace execution to figure out what a variable might refer to
[%inc dynamic.tll %] [%inc dynamic.out %]
Almost all languages used [%g lexical_scoping “lexical scoping” %]
Decide what a name refers to based on the structure of the program
More efficient for the computer: doesn’t have to search each time
More efficient for the person: limits scope of reasoning
More complicated to implement
But enables a very powerful programming technique
[%inc closure.py %] [%inc closure.out %]
The inner function [%g variable_capture “captures” %] the variables in the enclosing function
A way to make data private
[%inc adder.py %] [%inc adder.out %]
[% figure slug=“func-closure” img=“closure.svg” alt=“Closures” caption=“Closures” %]
[%inc oop.py %] [%inc oop.out %]
[% figure slug=“func-objects” img=“objects.svg” alt=“Objects as closures” caption=“Implementing objects using closures” %]
[% figure slug=“func-concept-map” img=“concept_map.svg” alt=“Concept map of functions and closures” caption=“Concept map.” %]