Skip to content

Instantly share code, notes, and snippets.

@duckinator
Created August 8, 2010 01:43
Show Gist options
  • Save duckinator/513450 to your computer and use it in GitHub Desktop.
Save duckinator/513450 to your computer and use it in GitHub Desktop.
[..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>
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