diff --git a/chapters/monte_carlo/code/haskell/monteCarlo.hs b/chapters/monte_carlo/code/haskell/monteCarlo.hs new file mode 100644 index 000000000..acbd2375a --- /dev/null +++ b/chapters/monte_carlo/code/haskell/monteCarlo.hs @@ -0,0 +1,14 @@ +import System.Random + +monteCarloPi :: RandomGen g => g -> Int -> Float +monteCarloPi g n = count $ filter inCircle $ makePairs + where makePairs = take n $ toPair (randomRs (0, 1) g :: [Float]) + toPair (x:y:rest) = (x, y) : toPair rest + inCircle (x, y) = x^2 + y^2 < 1 + count l = 4 * fromIntegral (length l) / fromIntegral n + +main = do + g <- newStdGen + let p = monteCarloPi g 100000 + putStrLn $ "Estimated pi: " ++ show p + putStrLn $ "Percent error: " ++ show (100 * abs (pi - p) / pi) diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index b9bd560f5..f7e9290a1 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -39,6 +39,8 @@ each point is tested to see whether it's in the circle or not: {% method %} {% sample lang="jl" %} [import:2-8, lang:"julia"](code/julia/monte_carlo.jl) +{% sample lang="hs" %} +[import:7-7, lang:"haskell"](code/haskell/monteCarlo.hs) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end, @@ -56,14 +58,14 @@ If we use a small number of points, this will only give us a rough approximation The true power of monte carlo comes from the fact that it can be used to integrate literally any object that can be embedded into the square. As long as you can write some function to tell whether the provided point is inside the shape you want (like `in_circle()` in this case), you can use monte carlo integration! This is obviously an incredibly powerful tool and has been used time and time again for many different areas of physics and engineering. -I can gaurantee that we will see similar methods crop up all over the place in the future! +I can guarantee that we will see similar methods crop up all over the place in the future! # Example Code Monte carlo methods are famous for their simplicity. It doesn't take too many lines to get something simple going. Here, we are just integrating a circle, like we described above; however, there is a small twist and trick. Instead of calculating the area of the circle, we are instead trying to find the value of $$\pi$$, and -rather than integrating the entire circle, we are only integrating the upper right quadrant of the circle from $$0 < x,y < 1$$. +rather than integrating the entire circle, we are only integrating the upper right quadrant of the circle from $$0 < x, y < 1$$. This saves a bit of computation time, but also requires us to multiply our output by $$4$$. That's all there is to it! @@ -73,13 +75,16 @@ Feel free to submit your version via pull request, and thanks for reading! {% sample lang="jl" %} ### Julia [import, lang:"julia"](code/julia/monte_carlo.jl) +{% sample lang="hs" %} +### Haskell +[import, lang:"haskell"](code/haskell/monteCarlo.hs) {% endmethod %} -$$ +$$ \newcommand{\d}{\mathrm{d}} \newcommand{\bff}{\boldsymbol{f}} \newcommand{\bfg}{\boldsymbol{g}}