Newsgroups: comp.lang.functional
Date: 31 Oct 2001 17:48:32 -0800
Local: Wed, Oct 31 2001 8:48 pm
Subject: Re: Non-local arithmetic. Was: Help! Which language is it?
Jerzy Karczmarczuk <karc...@info.unicaen.fr> wrote in message <news:3BDFE855.DF71664@info.unicaen.fr>... Actually, that _can_ be done. It is slow, unwieldy -- but eminently > > That might be a bit slow, but it would get the principle down. There > > are probably more efficient ways of representing things, and laziness > > might come in useful. > I am not sure about that last point. possible. Monads and lazy evaluation come in very handy. This article shows the example. The key insight is that we don't need to "symbolically" manipulate As an example, we compute the center and the variance of The framework first: import Monad -- An integer uniformly distributed within [0..32767] type RandomGen = RandomState -> RandomState newtype RandomVar a = RandomVar ( RandomGen -> RandomState -> (a,RandomState) ) instance Monad RandomVar where -- Other morphisms and monad constructors -- A random number uniformly distributed within [0,1] -- A random number uniformly distributed on [a, b] add:: (Num a) => (RandomVar a) -> (RandomVar a) -> (RandomVar a) sub:: (Num a) => (RandomVar a) -> (RandomVar a) -> (RandomVar a) mul:: (Num a) => (RandomVar a) -> (RandomVar a) -> (RandomVar a) divM:: (Num a,Fractional a) => (RandomVar a) -> (RandomVar a) -> (RandomVar a) sqrM:: (Num a) => (RandomVar a) -> (RandomVar a) sqrtM:: (Num a, Floating a) => (RandomVar a) -> (RandomVar a) -- standardized normal distribution N(0,1) -- N(z,var) -- square_root distribution -- Run the RandomVar monad n times and estimate mathematical expectation and run_random:: Int -> (RandomVar Float) -> (Float,Float) Now let's see what we've got well, close enough to the expected result. Main> run_random 5000 normal_stan Again, normal_stan indeed looks like a normally-distributed variable We can now do many wonderful things, such as Main> run_random 1000 $ add (normal 10 1) (normal 20 2) Back to the impedance however -- compute the impedance of an electric circuit impedance inductance capacitance freq resistance = -- inductance -- Frequency 10kHz - 20kHz -- capacitance -- resistance z = impedance ct_l ct_cap ct_freq ct_res Main> run_random 1000 z Here you have it: the average value and the variance. We can repeat You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||