
Hi folks, My primary language is R, which is an imperative functional language. I start to learn haskell and try to use it instead when a project is time-consuming and it takes months in R. I like the language very much so far, but I do miss some important functions in R which can generate random numbers from all kinds of distributions. R provides a standalone library in c which contains all these functions and I try to write a wrapper for haskell. Since I am very new to haskell, I want to hear some suggestions to confirm that I am on the right track. The functions are the following: void set_seed(unsigned int, unsigned int); void get_seed(unsigned int *, unsigned int *); they can set or get seed for random number generator. For any distribution, we have four functions for it -- Normal distribution double dnorm4(double x, double mu, double sigma, int give_log) double pnorm5(double x, double mu, double sigma, int lower_tail,int log_p) double qnorm5(double p, double mu, double sigma, int lower_tail, int log_p) double rnorm(double mu, double sigma) where dnorm calculates density, pnorm calculates p-value, qnorm calculates quantile, and rnorm generates normal random variables. dnorm, pnorm, qnorm are easy since they don't have side-effect. I think I can just use the following: foreign import ccall "dnorm4" dnorm :: Double -> Double -> Double-> Int -> Double foreign import ccall "pnorm5" pnorm :: Double -> Double -> Double-> Int -> Int -> Double foreign import ccall "qnorm5" qnorm :: Double -> Double -> Double-> Int -> Int -> Double (Should I use CDouble or CInt here?) But for rnorm, if I use "foreign import ccall rnorm :: Double -> Double -> IO Double", then my main function should carry IO monad all the time. Maybe the better way is that I should "encapsulate" it into a state monad rng -> (rng, randomnumber). Therefore for rnorm, I should first write a wrapper in c like seed2 rnorm_wrapper(seed1, parameters){ set_seed(seed1); rnorm(parameters); get_seed(seed2); } and then write another wrapper for rnorm_wrapper in haskell. Can somebody tell me if this is the right approach? Thank you! Best, Peng

time-consuming and it takes months in R. I like the language very much so far, but I do miss some important functions in R which can generate ^ typo ? ;) But for rnorm, if I use "foreign import ccall rnorm :: Double -> Double -> IO Double", then my main function should carry IO monad all the time. Maybe the better way is that I should "encapsulate" it into a state monad rng -> (rng, randomnumber). Therefore for rnorm, I should first write a wrapper in c like seed2 rnorm_wrapper(seed1, parameters){ set_seed(seed1); rnorm(parameters); get_seed(seed2); } set_seed rnorm get_seed doesn't have any side effect as long as you consider this sequence beeing atomic. If you don't use multithreading it should work fine. If you want to use multithreading have a look at http://haskell.org/haskellwiki/GHC/Concurrency, especially at "Software Transactional Memory (STM)" which is what you might need here (?) and then write another wrapper for rnorm_wrapper in haskell.
Can somebody tell me if this is the right approach? Thank you!
Have a look at (haskell.org -> library and tools -> Mathematics (http://haskell.org/haskellwiki/Libraries_and_tools/Mathematics) library "Probabilistic Functional Programming" Perhaps this lib does exactly what you need. There is another important source of packages: http://hackage.haskell.org/packages/archive/pkg-list.html Which way to go? I'm not a haskell expert so I can't tell you. It depends on what you need and how much time you want to spent on this topic ;) hope this helps Marc Weber

Thanks for the reply.
On 2/25/07, Marc Weber
time-consuming and it takes months in R. I like the language very much so far, but I do miss some important functions in R which can generate ^ typo ? ;) But for rnorm, if I use "foreign import ccall rnorm :: Double -> Double -> IO Double", then my main function should carry IO monad all the time. Maybe the better way is that I should "encapsulate" it into a state monad rng -> (rng, randomnumber). Therefore for rnorm, I should first write a wrapper in c like seed2 rnorm_wrapper(seed1, parameters){ set_seed(seed1); rnorm(parameters); get_seed(seed2); } set_seed rnorm get_seed doesn't have any side effect as long as you consider this sequence beeing
rnorm itself has side effect, right? It changes the seed for global random generator. That is why I think I need rnorm_wrapper to "purify" it.
atomic. If you don't use multithreading it should work fine. If you want to use multithreading have a look at http://haskell.org/haskellwiki/GHC/Concurrency, especially at "Software Transactional Memory (STM)" which is what you might need here (?)
and then write another wrapper for rnorm_wrapper in haskell.
Can somebody tell me if this is the right approach? Thank you!
Have a look at (haskell.org -> library and tools -> Mathematics (http://haskell.org/haskellwiki/Libraries_and_tools/Mathematics) library "Probabilistic Functional Programming" Perhaps this lib does exactly what you need.
I did see this, but I think it is not enough for me.
There is another important source of packages: http://hackage.haskell.org/packages/archive/pkg-list.html
Which way to go? I'm not a haskell expert so I can't tell you. It depends on what you need and how much time you want to spent on this topic ;)
hope this helps Marc Weber _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sun, 25 Feb 2007, Peng Zhang wrote:
Hi folks,
My primary language is R, which is an imperative functional language. I start to learn haskell and try to use it instead when a project is time-consuming and it takes months in R. I like the language very much so far, but I do miss some important functions in R which can generate random numbers from all kinds of distributions.
Maybe, also this one is of interest: http://haskell.org/haskellwiki/New_monads/MonadRandom
participants (3)
-
Henning Thielemann
-
Marc Weber
-
Peng Zhang