Skip to content

[Haskell] IFS #704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions contents/IFS/IFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ Here, instead of tracking children of children, we track a single individual tha
{% method %}
{% sample lang="jl" %}
[import:4-17, lang:"julia"](code/julia/IFS.jl)
{% sample lang="hs" %}
[import:7-13, lang:"haskell"](code/haskell/IFS.hs)
{% sample lang="cpp" %}
[import:39-52, lang:"cpp"](code/c++/IFS.cpp)
{% endmethod %}
Expand Down Expand Up @@ -193,6 +195,8 @@ In addition, we have written the chaos game code to take in a set of points so t
{% method %}
{% sample lang="jl" %}
[import, lang:"julia"](code/julia/IFS.jl)
{% sample lang="hs" %}
[import, lang:"haskell"](code/haskell/IFS.hs)
{% sample lang="cpp" %}
[import, lang:"cpp"](code/c++/IFS.cpp)
{% endmethod %}
Expand Down
30 changes: 30 additions & 0 deletions contents/IFS/code/haskell/IFS.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Data.Array ((!), Array, bounds, listArray)
import Data.List (intercalate)
import System.Random

data Point = Point Double Double

chaosGame :: RandomGen g => g -> Int -> Array Int (Point -> Point) -> [Point]
chaosGame g n hutchinson = take n points
where
(x, g') = random g
(y, g'') = random g'
choices = randomRs (bounds hutchinson) g''
points = Point x y : zipWith (hutchinson !) choices points

main :: IO ()
main = do
g <- newStdGen

let midPoint (Point a b) (Point x y) = Point ((a + x) / 2) ((b + y) / 2)
sierpinski =
listArray
(1, 3)
[ midPoint (Point 0 0),
midPoint (Point 0.5 (sqrt 0.75)),
midPoint (Point 1 0)
]
points = chaosGame g 10000 sierpinski
showPoint (Point x y) = show x ++ "\t" ++ show y

writeFile "out.dat" $ intercalate "\n" $ map showPoint points