[Haskell-beginners] Issue installing reactive-banana-5.0.0.1

Miguel Negrao miguel.negrao-lists at friendlyvirus.org
Tue May 8 22:03:07 CEST 2012


A 07/05/2012, às 13:12, Heinrich Apfelmus escreveu:

> Miguel Negrao wrote:
>> A 06/05/2012, às 14:31, Heinrich Apfelmus escreveu:
>>> Ah, ok, then I don't understand your specification.
>>> 
>>> Could you give a specification in terms of a simple list transformation
>>> 
>>> example :: [Double] -> [Double]
>>> 
>>> ? All list functions are allowed, we can then transform it into a
>> > style that uses only the combinators available in reactive-banana.
>> Ok, this should demonstrate an example of what I mean:
>> module Main where
>> main :: IO()
>> main = print $ test [0.9,0.1,0.2,0.8]
>> --should output [0.9,0.1,0.8,0.8]
>> test :: [Double]->[Double]
>> test (x:xs) = x : test1 xs x
>> test [] = []
>> test1:: [Double]->Double->[Double]
>> test1 (x:xs) lastValue = let
>> 	y = if lastValue>=0.8 then x else 1.0-x
>> 	in if (y<=0.2) || (y>=0.8) then y : test1 xs y else test1 xs lastValue
>> test1 [] _ = []   
> 
> You can reformulate this function in terms of the  mapAccum  combinators from Data.List. Once you have done this, you can easily adapt it to the  mapAccum  combinator from reactive-banana.
> 
>  test :: Event t Double -> Event t Double
>  test e = filterJust $ fst $ mapAccum Nothing $ next <$> e
>      where
>      next x Nothing          = (Just x, Just x)
>      next x (Just lastValue) =
>          let y = if lastValue>=0.8 then x else 1.0-x
>          in if (y<=0.2) || (y>=0.8)
>              then (Just y , Just y)
>              else (Nothing, Just lastValue)

Thanks, mapAccum is what I was missing. 

best,
Miguel


More information about the Beginners mailing list