Skip to content

Commit e0ece15

Browse files
jiegilletButt4cak3
authored andcommitted
Added Monte Carlo in Haskell (algorithm-archivists#119)
* Added Monte Carlo in Haskell and fixed some typos in the chapter
1 parent 7998183 commit e0ece15

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import System.Random
2+
3+
monteCarloPi :: RandomGen g => g -> Int -> Float
4+
monteCarloPi g n = count $ filter inCircle $ makePairs
5+
where makePairs = take n $ toPair (randomRs (0, 1) g :: [Float])
6+
toPair (x:y:rest) = (x, y) : toPair rest
7+
inCircle (x, y) = x^2 + y^2 < 1
8+
count l = 4 * fromIntegral (length l) / fromIntegral n
9+
10+
main = do
11+
g <- newStdGen
12+
let p = monteCarloPi g 100000
13+
putStrLn $ "Estimated pi: " ++ show p
14+
putStrLn $ "Percent error: " ++ show (100 * abs (pi - p) / pi)

chapters/monte_carlo/monte_carlo.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ each point is tested to see whether it's in the circle or not:
3939
{% method %}
4040
{% sample lang="jl" %}
4141
[import:2-8, lang:"julia"](code/julia/monte_carlo.jl)
42+
{% sample lang="hs" %}
43+
[import:7-7, lang:"haskell"](code/haskell/monteCarlo.hs)
4244
{% endmethod %}
4345

4446
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
5658
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.
5759
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!
5860
This is obviously an incredibly powerful tool and has been used time and time again for many different areas of physics and engineering.
59-
I can gaurantee that we will see similar methods crop up all over the place in the future!
61+
I can guarantee that we will see similar methods crop up all over the place in the future!
6062

6163
# Example Code
6264
Monte carlo methods are famous for their simplicity.
6365
It doesn't take too many lines to get something simple going.
6466
Here, we are just integrating a circle, like we described above; however, there is a small twist and trick.
6567
Instead of calculating the area of the circle, we are instead trying to find the value of $$\pi$$, and
66-
rather than integrating the entire circle, we are only integrating the upper right quadrant of the circle from $$0 < x,y < 1$$.
68+
rather than integrating the entire circle, we are only integrating the upper right quadrant of the circle from $$0 < x, y < 1$$.
6769
This saves a bit of computation time, but also requires us to multiply our output by $$4$$.
6870

6971
That's all there is to it!
@@ -73,6 +75,9 @@ Feel free to submit your version via pull request, and thanks for reading!
7375
{% sample lang="jl" %}
7476
### Julia
7577
[import, lang:"julia"](code/julia/monte_carlo.jl)
78+
{% sample lang="hs" %}
79+
### Haskell
80+
[import, lang:"haskell"](code/haskell/monteCarlo.hs)
7681
{% endmethod %}
7782

7883

0 commit comments

Comments
 (0)