Last active
August 29, 2015 14:01
-
-
Save ligfx/175f9fa5786e51526f2d to your computer and use it in GitHub Desktop.
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
| -- Run me with runhaskell gibbs.hs | |
| -- Compile me with ghc gibbs.hs -main-is Gibbs | |
| module Gibbs where | |
| import Prelude hiding (putStrLn) | |
| import qualified Data.ByteString.Lazy.Char8 as B | |
| import qualified Data.Aeson as Aeson (encode) | |
| import Data.Random (sampleFrom, RVar) | |
| import qualified Data.Random.Distribution.Normal as N (doubleStdNormal, normal) | |
| import Data.Vector (singleton) | |
| import System.Random.MWC as MWC (initialize) | |
| stdNormal = N.doubleStdNormal | |
| normal :: Double -> Double -> RVar Double | |
| normal = N.normal | |
| rollM :: Monad m => Int -> (a -> m a) -> a -> m [a] | |
| rollM 0 f initial = return [] | |
| rollM n f initial = do | |
| x <- f initial | |
| xs <- rollM (n - 1) f x | |
| return $ initial : xs | |
| genPref = rollM 100 (\x -> normal x 0.007) 0.49 | |
| main = do | |
| mwc <- MWC.initialize (singleton 14) | |
| pref <- sampleFrom mwc genPref | |
| B.putStrLn $ Aeson.encode pref |
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
| source("runhaskell.R") | |
| plot(runhaskellJ("Gibbs.main"), type="l") |
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
| library(stringr) | |
| runhaskell <- function(name) { | |
| module <- unlist(str_split(name, "\\.", n=2))[1] | |
| return(system(paste("export LD_LIBRARY_PATH=/usr/local/lib && echo 'import qualified ", module, "; main = ", name, "' | runhaskell"), intern=TRUE)) | |
| } | |
| library(jsonlite) | |
| library(functional) | |
| runhaskellJ <- Compose(runhaskell, fromJSON) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment