Langton's ant: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Replaced by more transparent and idiomatic solution)
m (→‎{{header|Haskell}}: some typos)
Line 2,929: Line 2,929:
<lang Haskell>data State = State { antPosition :: Point
<lang Haskell>data State = State { antPosition :: Point
, antDirection :: Point
, antDirection :: Point
, getMesh :: Set Point }</lang>
, getCells :: Set Point }</lang>


Now we are ready to express the main part of the algorithm
Now we are ready to express the main part of the algorithm
Line 2,949: Line 2,949:
task = iterate step
task = iterate step
>>> dropWhile ((< 50) . distance . antPosition)
>>> dropWhile ((< 50) . distance . antPosition)
>>> head >>> getPosition
>>> head >>> getCells
where distance (x,y) = max (abs x) (abs y)</lang>
where distance (x,y) = max (abs x) (abs y)</lang>


Line 2,959: Line 2,959:
main = display w white (draw (task initial))
main = display w white (draw (task initial))
where
where
w = InWindow "Ant" (400,400) (0,0)
w = InWindow "Langton's Ant" (400,400) (0,0)
initial = State (0,0) (1,0) mempty
initial = State (0,0) (1,0) mempty
draw = foldMap drawCell
draw = foldMap drawCell
Line 2,967: Line 2,967:
<lang haskell>main = simulate w white 500 initial draw (\_ _ -> step)
<lang haskell>main = simulate w white 500 initial draw (\_ _ -> step)
where
where
w = InWindow "Ant" (400,400) (0,0)
w = InWindow "Langton's Ant" (400,400) (0,0)
initial = State (0,0) (1,0) mempty
initial = State (0,0) (1,0) mempty
draw (State p _ s) = pictures [foldMap drawCell s, color red $ drawCell p]
draw (State p _ s) = pictures [foldMap drawCell s, color red $ drawCell p]