I like to think about pure function as a transmutation of inputs. Just like in Alchemy you connect Fire and Water to get Steam. Fire and Water are inputs, Steam is output. Or treat function as an assembly machine -- it takes resources, and converts those into another resource.
After that try posing a question: given two integer numbers as an input, what kinds of transmutation about these two numbers can you think about?
So first idea should be:
- add numbers to get a number
- multiply numbers
- substract numbers
- divide numbers
And usually that's it, I mean, student fantasy often stops here. Now your turn to make "how did I not think about it?" in student's brain:
- substract first number from second (change argument order)
- modulo divide two numbers
- integer part of division
- exponentiate numbers
- treat numbers as triangle sides and compute hypotenuse by Pythagorean rule
- min or max of two
Make sure students understood what every of these functions had done to two numbers. Ask them "anything else?" and if they produce more math examples (combinations), this is good, but at some step stop them and show this:
- drop second (i.e., function which takes two arguments, and returns only first)
- drop first
- return those two back as a 2-tuple
- return those two back as a swapped 2-tupple
- return nothing
Now again, ask students "anything else?", make sure they understand how a function can take two arguments and do nothing, and return nothing, this is quite counterfactual for some. It is good if some list results are guessed, otherwise, show yourself:
- return list, where first arg is repeated second arg times
- range of numbers from min arg to max arg
- sum of consecutive numbers from min arg to max arg
- numbers from 1 to first arg with second arg step
- numbers from 1 with first arg as step and second arg item count
- first arg count primes, each divided by second arg
Make sure students are able to visualize each example, of how two numbers transform to list. The biggest sign of this is when they get the pattern and try to produce own examples. After some time, you break their brain again:
- a list where number 42 is repeated first arg times, and then second arg is repeated 200 times
- a list where first arg is repeated 3 times, then second arg is repeated 3 times, then first arg is repeated 4 times
- a list where 3 is repeated first arg times, then 0 and this whole pattern is repeated second arg times x5
Ask students "anything else?", and if they grok pattern they should start some random list generations with some random numbers. The more random examples, the better, creativity is not easy. But at some point you break their brain once more:
- given two args, return a function which will take one more arg and return sum of three args
- given two args, return a function which will take one arg and ignore previous two args alltogether, and return square of an argument
- given two args, return a function which takes list as an argument, and returns range of numbers from this list using two previous args
It is mind-shifting to think about function which returns a function, but if students can provide own examples (however fantastic), this is very good sign. Good if somebody can create function which returns a function which returns a function. But if not:
- takes 2 args, returns a function, which takes no args and returns a function which takes 1 arg and returns pair of previous 2 args each multiplied by arg in innermost function (sic!)
- return a function, which takes a function as an argument, and returns application of this arg function to previous 2 args
- return a function, which takes a function as an argument, applies it first arg, and then applies itself to this result, and continues this process second arg times
After students brain is completely devastated by trying to imagine this, you show a few more examples:
- compare two numbers and return "less", "greater", "equal"
- return True if both are greater than 42, False otherwise
- convert numbers to strings and return them combined
- translate these numbers to Mongolian and return a tuple of string representations
- ... show creativity!
Ok, and after all this done, ask the original question again "what can a function do with two numbers" and check student recall, how many of these examples were really grokked. The simple def plus(a, b): return a + b
will now look different in student's head
P.S. After lesson is done, you tell "all the examples we talked about were about pure functions. But if we consider impure functions, then ..." :)