Last active
August 29, 2015 14:03
-
-
Save samrat/2ac4ca588e36aa2f840f to your computer and use it in GitHub Desktop.
Random walk in Elm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Random | |
(w,h) = (600, 400) | |
initialPosition = (0, 0) | |
intToDir d n = | |
if d < 0.5 | |
then n | |
else -n | |
step : Signal Float -> Signal (Float, Float) | |
step s = | |
let dx = Random.float s | |
dy = Random.float s | |
dirX = Random.float s | |
dirY = Random.float s | |
in lift2 (,) (lift2 intToDir dirX dx) (lift2 intToDir dirY dy) | |
translate (x, y) (dx, dy) = (x+dx, y+dy) | |
walk = foldp (\delta acc -> (translate (head acc) delta) :: acc) | |
[initialPosition] | |
(step (fps 100)) | |
draw p = collage w h | |
[ traced (solid lightBlue) <| path p ] | |
main = lift draw walk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment