In Chapter 13 you learned how to progam dynamic, graphical animations in a highly abstract, declarative manner. But there is one thing missing: reactivity. That is, the ability of our animations to react to user input or other external stimuli. In Chapter 10 you learned how user input could alter a graphics image, so at first blush, doing so for animations would seem to be just as easy. The problem is, we have lifted our thought processes and programming style to a level where we are manipulating continuous, never-ending animations, and the notion of a discrete event, such as a mouse press, does not fit in so well. For example, try to write a program, at the level of Behaviors, for a simple ball that changes color from red to blue when the mouse is pressed. I think that you will find this to be quite difficult.
In solving this problem I will borrow some ideas from a (nonstandard) Haskell library called Fran, which is short for functional reactive animation. Fran, and the version of it that we will develop in this chapter, are examples of domain specific languages, or DSLs. A DSL is a language that is designed for a specific application domain, in this case reactive animations. Haskell is an excellent vehicle for directly implementing DSLs without having to create a new language from scratch. In this way we say that the DSL is embedded in Haskell, or that Haskell is the host language for the DSL. To distinguish our language from Fran, I will call it FAL, for functional animation language.
I will begin by describing FAL by example. I will then describe a partial implementation of FAL based on streams which, because of its simplicity, can also serve as its specification. Once this foundation is established, I will describe various extensions to FAL, culminating in a final example, a twenty-line program to play the game of paddleball.
Review the options below to login to check your access.
Log in with your Cambridge Aspire website account to check access.
If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.