[Haskell-beginners] FRP

Ertugrul Söylemez es at ertes.de
Sun Sep 16 09:16:19 CEST 2012


Christopher Howard <christopher.howard at frigidcode.com> wrote:

> Hi. I'm trying to understand what Functional Reactive Programming is,
> or, more properly, what distinguishes it from "regular" functional
> programming, and what it would look like if I were to program in a
> "FRP Style", i.e., without some mysterious FRP module hiding the
> details. After reading a bunch of links and even looking at some
> source code I'm still not clear on this.
>
> If I program in an event-based style, and have data structures
> updating over time, with some data structures dependent on others for
> their values, and I use a functional language, is that FRP? Or is
> there some essential element I'm missing here?

Yes.  FRP is about domain-specific languages that capture the notion of
time-varying values.  Let me take Netwire 4 as an example.  It's not on
Hackage yet, so you may want to grab it using darcs:

    darcs get http://darcs.ertes.de/netwire/

Imagine you have a simple GUI label that displays the number of seconds
passed since program start.  In an event-based model this is actually
quite a complicated task.  You would have to create a label and update
it all the time using some form of timer/idle event.  In Netwire you
write:

    myLabel = time

Now let's say you want to have the same GUI, but the time should start
at 10 and pass twice as fast, so you actually want to display twice the
number of seconds passed plus 10:

    myLabel = 10 + 2*time

Imagine you want to display the string "yes" in a label:

    myLabel = "yes"

Now let's say you want to display "yes", when the space key is held down
and "no" otherwise:

    myLabel = "yes" . keyDown Space <|> "no"

You want to display time while pressed and "Press space" while not:

    myLabel = fmap show time . keyDown Space <|> "no"

You want to display "yes" every other second and "no" otherwise:

    myLabel = "yes" . holdFor 1 (periodically 2) <|> "no"

Imagine doing that with event-based code.

Summary:  FRP is about handling time-varying values like they were
regular values.


Greets,
Ertugrul

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120916/522c5fb1/attachment.pgp>


More information about the Beginners mailing list