Introduction to Systematic Programming – Part 7, Locals & Abstraction
Join the DZone community and get the full member experience.Join For Free
week 7 brought in the concepts of both local and abstract expressions. as seems to be the case with me, this week was half a review from other courses that was nice and refreshing, and then bam, a new concept that completely threw me for a loop. not complaining about this per-se, because i think it’s a great way to add things to my concept toolbox (reviewing old things and then throwing in something new), but it *is* a bit of a rollercoaster.
lesson 1: introduction to local expressions
the first exciting element in this week is another language upgrade! now we’re moving into intermediate student language. score.
as far as local expressions, we learn in this introductory lesson that local expressions are expressions that have functions and constants that only work in one part of a program. that’s all we get here. more to come on this later...
lesson 2: local expressions – forming & intuition
upgrading our lives to intermediate student language gives us the ability to utilize local expressions, as vaguely defined in lesson 1. basically, local expressions are small, packaged expressions with their own functions and variables that cannot interact with the rest of the program, outside of their local container. here’s the anatomy of a local:
in this example, both of the “defines” are definitions for variables that will only exist within this local container, meaning that if “a” is referenced on the top level of the program, aka, outside of this local, whatever we do to a within this local does not matter to the other instances of “a”. why? because if “a” is used in a local, it only exists within that local, and has no interaction with any other “a” that might be within the program. this means you can also use “a” in all of your locals, and nothing will happen between them, as long as you structure your local correctly.
lesson 3: local expressions – lexical scoping
scope contours answer your questions about where you can access your local definitions. lexical scoping states, essentially, that you use the definition of a function or variable that is within the innermost inclosing area to where your expression is being evaluated.
lesson 4: local expressions – evaluation rules
as previously mentioned, locals are expressions, they don’t affect the things outside of their purview. here are the steps for evaluation!
- take the definitions in the local, find all the references to them, come up with new, globally unique names for them and replace all instances of the old name with this new, unique one.
- move the renamed definitions into the top level.
- replace the entire local with just its body, with the renamed definitions.
it’s interesting to note that by evaluating your local expression in this fashion, you’re actually eliminating its existence as a local, and thus your program could theoretically have been written in the basic student language!
lesson 5: local expressions – encapsulation
encapsulation is taking each part of a program and turning it into a little package that only interacts with a few small items on the top level. essentially, it is making single functions with locals inside them. when you encapsulate a function, it’s important to remember that you can no longer access it globally. however, this also means that as an encapsulated function, you don’t have to write signatures and tests, which will end up saving you lots of time. but, you may still want to test them, so don’t forget about doing that!
how can you tell if a function is a good candidate for encapsulation? well….
- does it exist as one function with closely linked helpers?
- does the outside program really only need to call the one main function, not the helpers?
if you answered yes to both of these, you’re likely looking at a great candidate for encapsulation!
how do you encapsulate a function?
- group all functions you want to encapsulate together.
- open your definition with a new global function name and the necessary parameters.
- open a local expression and then the definition part of the local with [, right before the original function definition.
- close the definition part with a ] after the original function definition.
- add a trampoline call of the function, defined in the local, to the end.
- delete unnecessary tests, signatures and stubs.
- rename the tests and necessary stubs
- run free! you got it!
it’s also important to note that you can have encapsulation in your templates, but keep in mind that if you do this, it will disallow your ability to test for a base case.
additional notes from this lesson:
another topic covered in this lesson is refactoring, which is a structural change to a program that does not change the behavior of the program. you can, however, refactor something and also change its behavior, but it’s not recommended to make both of these changes at the same time (it’s easier to make mistakes by trying to do too much at once!).
lesson 6: local expressions – avoiding recomputation
finally we’re getting to the concept of code efficiency in this class. yay! this lesson begins with the idea that you should worry about efficiency of code after your program works. in some ways i agree with this, particularly for beginners, but i also disagree a bit. i think code efficiency should be engrained from the beginning of your coding career, so it becomes a natural way of thinking, rather than an afterthought. that said, this lesson focuses on eliminating exponential growth. essentially, the solution here is very situational, so i don’t want to go into it too much. however, the takeaway is that when you’re working with arity trees, if you can localize your searching, it will make your code more succinct and efficient. j
lesson 7: introduction to abstraction
abstraction, in programming, is the way to manage complexity in your program. #iabstractedthatlessonforyou
lesson 8: abstraction from examples, part 1
when you’re abstracting, you need to pull out the common parts of functions into abstract functions. (note: the differing portions are called “points of variance.”)
in a function, you rename it as more general, put a parameter to represent the point of variance, and replace the point of variance with the parameter that you renamed (remember to pass the parameter in any recursions as well!).
why would you want to abstract functions?
a higher order function can consume more than one function. it can also produce a function.
so, get used to it. it’s better. promise.
lesson 9: abstraction from examples, part 2
lesson 9 mostly works through a continued example from lesson 8, so i’m going to operate as usual and get rid of the example run-through and give you the important takeaways:
- functions can consume other functions as arguments.
- abstraction exists and works in the majority of programming languages.
- for abstractions, statements of purpose are very difficult to write, so start practicing.
lesson 10: abstraction from examples, part 3
writing the signature comes last when doing an abstraction, because it’s even more difficult than writing a statement of purpose. here’s a tip on how to work through it: use type inference! type inference goes a little like this:
- find the most basic thing that a function produces, that becomes the result in the signature.
- find the arguments that are passed/required. those are the argument types for the signature.
it sounds easy, but there’s so much going on with abstraction that it can be pretty disconcerting. don’t get discouraged, just try and focus on simplifying what you’re working on, as you do with the entire design recipe theory, and you’ll be just fine.
additional notes from this lesson:
- when a function is passed as an argument, write its signature in () in the larger signature.
- lists can be written in an alternate way. you can now use (listof x) in place of listofx
lesson 11: using built-in abstract functions
map and filter are two of the more commonly used built-in abstract functions, and the language page in the coursera course lists all of them. (if you’re not taking the coursera class, here’s an external link you can check out with the same info: bsl built-in abstract functions .)
the built-in abstracts are pretty sweet, and the reality is, if it’s pretty generic feeling, trust your intiuition and check out the list – it probably is a built-in!
lesson 12: closures
sometimes the function you’re passing as an argument to an abstract function doesn’t exist yet…. if this is the case, you can always define it with a local.
in some cases, you don’t have a choice. you must stop and define it.
when is that?
when the body of a function you want to pass to an abstract function refers to a parameter in the outer function. the function definition of the outer function thus closes around the inner one, making it a closure. (remember that lexical scoping? yeah!)
lesson 13: abstract fold functions
this lesson gives a quick overview on going directly from templates to abstract functions, literally cutting out all of the goo in the middle, optimizing your time writing your code. yay. in short, a fold function is an abstract function based directly on the template.
week 7 summary
as i mentioned at the start, this lesson was looooooooooooooooooong in the second half, because a lot of it was new to me. if you have any tips or tricks of your own related to abstract functions, let me know! also! week 8 is the last week of class so the next blog will be the last in the series. don't worry though, i have lots of plans for upcoming entries. :)
Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.