Skip to content

Instantly share code, notes, and snippets.

@jneira
Forked from puffnfresh/RandomWalk.hs
Created April 11, 2014 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jneira/10440627 to your computer and use it in GitHub Desktop.
Save jneira/10440627 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Monad.Trans (lift)
import Control.Parallel (par, pseq)
import Data.Text.Lazy (pack)
import System.Random (StdGen, newStdGen, random)
import Web.Scotty (get, scotty, text)
randomWalk :: Int -> Int -> StdGen -> Int
randomWalk s 0 _ = s
randomWalk s i r = randomWalk s' (pred i) g
where (b, g) = random r
s' = (if b then succ else pred) s
main :: IO ()
main = scotty 3000 $ do
a <- lift $ newStdGen
b <- lift $ newStdGen
get "/" $ do
let walk = randomWalk 0 10000000
first = walk a
second = walk b
text . pack . show $ first `par` second `pseq` (first + second)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment