Anonymous user
Langton's ant: Difference between revisions
→{{header|Ela}}
(add scala) |
|||
Line 782:
A straightforward implementation (assumes that we start with ant looking forward):
<lang ela>open list
type Field = Field a
field s = Field [[White \\ (_) <- [1..s]] \\ (_) <- [1..s]]▼
type Color = White | Black
type Direction = Lft | Fwd | Rgt | Bwd
isBlack Black = true
isBlack _ = false
newfield xc yc (Field xs) = Field (newfield' 0 xs)
where newfield' _ [] = []
newfield' n (x::xs)
| n == yc = row
| else = x :: newfield' (n+1)
row n
| n == xc
| else = x :: row (n+1)
where toggle White = Black
toggle Black = White
where show' [] = ""▼
showPath (Field
where show'
show'
showRow sb (x::xs) = showRow (sb +> s)
| else = "_"
move s xc yc = move' (Fwd,xc,yc) (field s)
where move' (pos,xc,yc)@coor fld
| xc >= s or yc >= s or xc < 0 or yc <
|
| else
matrix
matrix
go
go
right
left Rgt = Fwd
left Fwd = Lft</lang>
This implementation is pure (doesn't produce side effects).
|