Iteration

while Statements

Execution rule:

  1. Evaluate the header’s expression.
  2. If it’s a true value, execute the (whole) suite, then return to step 1.

Iteration Demo - The Fibonacci Sequence


Designing Functions

Three characteristics of functions that are really important:

  • A function’s domain is the set of all inputs it might possibly take as arguments.
  • A function’s range is the set of output values it might possible return.
  • A pure function’s behavior is the relationship it creates between input and output.

A Guide to Designing Functions

  • Give each function eactly one job, but make it apply to many related situations
  • Don’t repeat yourself. Implement a process just once.
>>> round(1, 23)
1
>>> round(1.23, 1) # round to 1 decimal place
1.2
>>> round(1.23, 0) # roudn to integer
1

Generalizing Patterns with Arguments

The area example

Higher Order Functions

Generalizing over Computational Processes

A higher order function is a function that takes in another function as an argument.

Example, summation and make_adder

Locally Defined Functions

Functions defined within other function bodies are bound to names in a local frame.

An operator is any expression that evaluates to a function. So, in

make_adder(1)(2)

make_adder(1) is the operator.

Hey, kudos for making it this far! Wanted to let you know that if you liked this, you might also like tmux Cheatsheet and Shortcuts.

ABOUT THE AUTHOR RSS Feed Icon

I'm Bobby Yan, a programmer, music lover, researcher, and a third-year undergraduate student at UC Berkeley majoring in Electrical Engineering and Computer Science. I am currently doing research on robotics, planning, and systems for machine learning at RISELab. In the past, I built IB Notes, a website with useful and concise study guides and notes for IB, and Spanish Vocab Builder, an iOS app to help Spanish learners. See my résumé for more details.

If you enjoyed this article, you should follow me on Twitter or sign up to get an email whenever I write something new:

Share this article with your friends: