[Haskell-cafe] Is this a correct explanation of FRP?
Peter Minten
peter.minten at orange.nl
Thu Mar 29 19:15:24 CEST 2012
Hi,
I've been trying to get my head around Functional Reactive Programming
by writing a basic explanation of it, following the logic that
explaining something is the best way to understand it.
Am I on the right track with this explanation?
Greetings,
Peter Minten
P.S. Sorry about the long mail, the explanation ended up a little longer
than I originally expected. :)
Document (with markdown formatting) follows:
--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--
This is an attempt to explain Functional Reactive Programming (FRP)
enough to give a reader with no previous exposure to FRP an intuition
what FRP is about. After reading this you should hopefully understand
enough of FRP to understand the
[reactive-banana](http://www.haskell.org/haskellwiki/Reactive-banana)
examples.
FRP has certain terms such as behavior, event and time-varying that can
be confusing for people unfamiliar with it. I'll avoid these terms at
first and will focus on spreadsheets and a generalization of spreadsheet
cells (which I will call boxes). Later, once the most important concepts
are explained, reactive-banana syntax will be introduced along with an
example that demonstrates how to work with behaviors and events in
reactive-banana. Finally some theory about time-varying functions and
how events and behaviors can be implemented using pure functions by
making time explicit should provide the necessary background to
understand reactive-banana's haddock comments.
The version of reactive-banana used here is
[0.5.0.0](http://hackage.haskell.org/package/reactive-banana-0.5.0.0).
Reactive Programming for the Masses: The Spreadsheet
====================================================
Spreadsheets are something we all (for certain values of we) know about.
Let's talk about a typical, simplified, spreadsheet. We have a list of
products that we sell and want to compute their price with the Value
Added Tax (VAT) added. We might have cells A1 to A10 contain the raw
prices of our products and cell B1 contain the current VAT rate (say 19
for a 19% VAT). In cells C1 to C10 we'd like to see the prices including
VAT.
In cell C1 we'd have a formula: `=A1*(1+B1/100)`, in cell C2
`=A2*(1+B1/100)`, etc. So if A1 contains $100 C1 would contain $119.
But what if the government, in it's eternal quest to reduce the budget
deficit, raises the VAT rate? We'd adjust cell B1, just change it to 20.
And like magic all the C cells are updated.
Though this may seem mundane what we've just seen is actually a very
good example of reactive programming. We didn't tell the C cells to
update; they updated on their own because a value they depend on
changed.
More information about the Haskell-Cafe
mailing list