[Index of Lectures]

[ L01 L02 L03 L04 L05 L06 L07 L08 L09 L10 L11 L12 L13 L14 L15 L16 L17 L18 L19 L20 L21 L22 L23 ]

Lecture 12—Local Expressions

Before Lecture

In this lecture you will learn how to use local expressions, a new language construct available in the Intermediate Student Language. How local expressions are formed and evaluated is covered in the following edX videos. It is critical you understand this before coming to lecture, because the lecture itself will be focused on how to use local expressions.

During Lecture

There are three principle ways we use local expressions: avoiding recursive calls with identical arguments, encapsulating 2 or more functions, and giving names to intermediate values. We will cover the first two in lecture and you will work through the third after lecture.

The starters for this lecture are:

After Lecture

Today we went through the Local module. You learned before class what local means and how it is evaluated. In class we saw three important ways to use it. For all three of those we saw a refactoring that allows you to take a working program and revise it, in a few simple steps, to be an improved program using local. They were:

We saw that for templates involving natural mutual recursions you can pre-encapsulate the templates, rather than waiting until the functions are designed and then encapsulating them.

We also saw that when we do encapsulate a set of functions, we are not able to test the inner functions. BUT, we can still write commented out examples to help us understand how to combine those results though! We can write notes beside the expressions in our body to help us reason about what it would resolve to including the type and what we expected the result to be). Such nodes are in the solution posted for you use. You can also put @signature and @template-origin tags right in the local expression. The edX videos go through a full HtDF process for these inner functions directly - this is not necessary but again it can be VERY helpful!

At the very end we saw that sometimes we use local for two different purposes in the same function -- encapsulation and avoiding redundant calls to a recursive function.

There's a third way to use local, which is to give meaningful names to intermediate values. Used properly this can make an otherwise complex function easier to understand. You should carefully compare the intermediate and final versions of the lines function in the solution file.

In summary there is a language construct called local, that has well defined evaluation rules; there are also three classic ways of using local. When you decide to use local, you should have a clear sense of what you are using the local to accomplish.

To review today's material re-do the examples from class and complete all the starter files. For more practice, look in the problem bank for more problems. Local L1 has a video that goes along with it if you need more help. The other problems are good practice as well.

The solutions for this lecture are:

Be sure to complete the before lecture section of Lecture 13 before that lecture.