[Haskell-beginners] Issue installing reactive-banana-5.0.0.1
Heinrich Apfelmus
apfelmus at quantentunnel.de
Mon May 7 14:12:50 CEST 2012
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)
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
More information about the Beginners
mailing list