[Haskell-beginners] FRP

Heinrich Apfelmus apfelmus at quantentunnel.de
Mon Oct 1 10:49:12 CEST 2012


Miguel Negrao wrote:
> 
> Thanks for the explanation. I was wondering, how would one translate
> this Yampa code into reactive-banana:
> 
> fallingBall :: Pos -> Vel -> SF () (Pos, Vel)
> 	fallingBall y0 v0 = proc () -> do
> 		v <- (v0 +) ˆ<< integral -< -9.81
> 		y <- (y0 +) ˆ<< integral -< v
> 		returnA -< (y, v)
> 
> fallingBall’ :: Pos -> Vel -> SF () ((Pos,Vel), Event (Pos,Vel))
> fallingBall’ y0 v0 = proc () -> do
> 	yv@(y, _) <- fallingBall y0 v0 -< ()
> 	hit <- edge -< y <= 0
> 	returnA -< (yv, hit ‘tag‘ yv)
> 
> bouncingBall :: Pos -> SF () (Pos, Vel)
> bouncingBall y0 = bbAux y0 0.0
> 	where
> 		bbAux y0 v0 = switch (fallingBall’ y0 v0) $ \(y,v) -> bbAux y (-v)
> 
> Would it be possible to do this without dynamic event switching ?
> What  about with the new event switching in v0.7 ? Also, is it possible (and
> is it easy ?) to do looping such as it is done in Yampa using the the
> loop arrow in reactive-banana/classic FRP ?

The Animation.hs example may help

   http://www.haskell.org/haskellwiki/Reactive-banana/Examples#animation

Essentially, the main difference is that reactive-banana doesn't have 
functions like  integral  or  edge  because they depend on an implicit 
time step. In reactive-banana, you have to manage time yourself, for 
instance by making a timer event from a wxTimer.

To calculate velocity and position, you can you use  accumE  on the 
timer event. To do collision detection, you can check whether the ball 
will move below the floor at each time step. Dynamic event switching is 
not needed here.

For a more complex example, have a look at Andreas Bernstein's paddle 
ball game

   https://github.com/bernstein/breakout



Best regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com




More information about the Beginners mailing list