Created
August 8, 2010 01:43
-
-
Save duckinator/513450 to your computer and use it in GitHub Desktop.
This file contains 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
[..e/nick/dev/haskell/markov]$ ghci | |
GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help | |
Loading package ghc-prim ... linking ... done. | |
Loading package integer-gmp ... linking ... done. | |
Loading package base ... linking ... done. | |
Loading package ffi-1.0 ... linking ... done. | |
Prelude> :l markov.hs | |
[1 of 1] Compiling Main ( markov.hs, interpreted ) | |
Ok, modules loaded: Main. | |
*Main> let x = markovChain 1 5 | |
Loading package old-locale-1.0.0.2 ... linking ... done. | |
Loading package time-1.1.4 ... linking ... done. | |
Loading package random-1.0.0.2 ... linking ... done. | |
*Main> x | |
[-5,-1] | |
*Main> x | |
[-5,-1] | |
*Main> x | |
[7,1] | |
*Main> |
This file contains 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
import System.Random | |
--randomNegate :: (Integral a) => a -> IO a | |
--markovMain :: (Integral a) => a a -> [a a] | |
--markovChain :: (Integral a) => a a -> [a a] | |
randomNegate magnitude = do | |
rand <- randomIO :: IO Bool | |
if rand | |
then return $ negate magnitude | |
else return magnitude | |
markovMain initial range n = do | |
up_or_down <- randomNegate 1 | |
let n1 = n + up_or_down | |
if (n1 > (initial + range)) || (n1 < (initial - range)) | |
then return [n1, up_or_down] | |
else markovMain initial range n1 | |
-- markovMain expects `initial range n`, but n starts at initial | |
markovChain initial range = markovMain initial range initial |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment