Skip to content

Instantly share code, notes, and snippets.

@samrat
Last active August 29, 2015 14:03
Show Gist options
  • Save samrat/2ac4ca588e36aa2f840f to your computer and use it in GitHub Desktop.
Save samrat/2ac4ca588e36aa2f840f to your computer and use it in GitHub Desktop.
Random walk in Elm
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