Since the concept is a little abstract, and I wasn't sure if I fully believed it myself, I wrote a simple program in Python that has four functions that call each other. For those of you new to reading Python code, here are the key things to know:
- Every function starts with a name and inputs. To declare that it is a function, "def" is used to start the line. Then the function name follows. Finally, in the parentheses, are the input(s) passed into the function.
- At the end of a function that computes something and wants to send it back, you need to create a "return" statement. This simply sends back the answer to the place that called it.
- On each line with an equal sign, the statement to the right is executed first. Then it is saved in the variable on the left of the equal sign for later use.
- The statement "for i in range(__)" is a Python way of saying "loop __ times". The things that are looped are indented one extra tab from the "for..." line.
- Finally, the "#" sign is not a hashtag! It starts a comment on that line. This means that everything after the "#" is NOT code, but an explanation of what is going on for the reader. When writing the code on my computer, the text editor changes its color (as I have also done here) to help you notice that difference.
def count(initial): # the count function takes an initial value
return initial + 1 # it adds one and return the result
def add(val_1, val_2): # the add function takes any two positive integers
answer = val_1 # start counting from the first value
for i in range(val_2): # the number of times to count forward in the loop
answer = count(answer) # count once on the old answer
return answer # all done!
def multiply(val_1, val_2): # the multiply function takes any two positive integers
answer = 0 # start the repeating adding from 0
for i in range(val_2): # the number of times to add in the loop
answer = add(answer, val_1) # add once on the old answer
return answer # all done!
def power(base, exponent): # the power function take base and exponent
answer = 1 # start the repeat multiplying from 1
for i in range(exponent): # the number of times to multiply in the loop
answer = multiply(answer, base) # multiply once on the old answer
return answer # all done!
When I call "add(7,5)", it returns "12". This required the program to call "count" 5 times. When I call "multiply(3,6)", it returns "18". This required the program to call "add" 6 times, which in turn resulted in 18 calls to "count". Finally, when I call "power(4,3)", I get "64". This required 3 calls to "multiply", which made 12 calls to "add", which made 84 calls to "count". The point is that the higher order functions all rely on the ones below them and just add one more layer of repetition.
If you're reading this, I hope this sparks some new ideas for you! I would also love to hear if you have ideas on extending this. It would be so cool to fully build up algebra in a computational world!
I like this view of algebraic operations because it helps to support pemdas (well, at least emdas) Exponents are the most repetitions of adding, Multiplication is middle, and Addition is a single instance, and along the way, we have ways of undoing the repetition.
ReplyDeleteYou're totally right with order of operations -- cool observation! I guess you could say powers are a higher order operation (more looping sequences), and thus come first.
Delete