Skip to content

Instantly share code, notes, and snippets.

@DeTeam
Created May 19, 2014 08:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeTeam/ad2f8cfcab34c0ecd459 to your computer and use it in GitHub Desktop.
Save DeTeam/ad2f8cfcab34c0ecd459 to your computer and use it in GitHub Desktop.
State-based oscilator in haskell
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