Created
May 19, 2014 08:22
-
-
Save DeTeam/ad2f8cfcab34c0ecd459 to your computer and use it in GitHub Desktop.
State-based oscilator in haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad.State.Lazy | |
step start stop (currentValue, modifier) = (currentValue, newState) | |
where reverseModifier = (currentValue + modifier > stop) || (currentValue + modifier < start) | |
modifier' = if reverseModifier | |
then | |
-modifier | |
else | |
modifier | |
newValue = currentValue + modifier' | |
newState = (newValue, modifier') | |
osci :: Int -> Int -> State (Int, Int) Int | |
osci start stop = state stepper | |
where stepper = step start stop | |
main :: IO () | |
main = print $ evalState (replicateM 20 osciFromOneToFive) (1, 1) | |
where osciFromOneToFive = osci 1 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment