Langton's ant: Difference between revisions

(→‎{{header|REXX}}: added comments, DO-END labels, remove superflous blanks. -- ~~~~)
Line 777:
...........................................................................
...........................................................................</pre>
 
=={{header|Ela}}==
 
A straightforward implementation (assumes that we start with ant looking forward):
 
<lang ela>open Core
 
let field s = Field [[White \\ (_) <- [1..s]] \\ (_) <- [1..s]]
 
let newfield xc yc (Field xs) = Field (newfield' 0 xs)
where newfield' _ [] = []
newfield' n (x::xs) | n == yc = row 0 x :: xs
| else = x :: newfield' (n+1) xs
where row _ [] = []
row n (x::xs) | n == xc = toggle x :: xs
| else = x :: row (n+1) xs
where toggle White = Black
toggle Black = White
 
let extends show (Field xs) = show' xs
where show' [] = ""
show' (x::xs) = showRow x ++ "\r\n" ++ show' xs
where showRow [] = ""
showRow (x::xs) = s ++ showRow xs
where s | x is White = "_"
| else = "#"
 
let 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 < 0 = fld
| isBlack fld = fld |> newfield xc yc |> move' (matrix Lft coor)
| else = fld |> newfield xc yc |> move' (matrix Rgt coor)
where isBlack (Field xs) = (xs:yc):xc is Black
et matrix Lft (pos,x,y) = go (left pos,x,y)
matrix Rgt (pos,x,y) = go (right pos,x,y)
et go (Lft,x,y) = (Lft,x-1,y)
go (Rgt,x,y) = (Rgt,x+1,y)
go (Fwd,x,y) = (Fwd,x,y-1)
go (Bwd,x,y) = (Bwd,x,y+1)
et right Lft = Fwd
right Fwd = Rgt
right Rgt = Bwd
right Bwd = Lft
et left Lft = Bwd
left Bwd = Rgt
left Rgt = Fwd
left Fwd = Lft
</lang>
 
This implementation is pure (doesn't produce side effects).
 
Testing:
 
<lang ela>show <| move 100 50 50</lang>
 
Output (empty lines are skipped to save space):
 
<pre>__________________________________________##__############__##______________________________________
_________________________________________#__####__________#__##_____________________________________
________________________________________###___##____________##_#____________________________________
________________________________________#_#__#_________#__#____#____________________________________
____________________________________##__##_#_#_________###_______#__________________________________
_________________________________###_#__#___#_____#_____##_##__###__________________________________
__________________________________#_#__###__##_####_##___#_#__#_##__##______________________________
__________________________________#_###_##__#_##__###_#_#_____###___###_____________________________
________________________________#_____#___#####_#_#__####__#___###_#_#_#____________________________
_______________________________###_##___#_####__##_##_######_#_###_#___#____________________________
_______________________________#_###_#_##_#_#_##_##_##_#___#####_###_##_____________________________
___________________________________#_#___#_##_###___#___#_#__####____#_##___________________________
________________________________#__#_________##_##___#__##_____##_#_____##__________________________
_______________________________###___#_#_##_###__#__##_____#___###_##__##_#_________________________
______________________________#__###__##___##_##___###__#____#__##_####___#_________________________
_____________________________###___#___#_#__#_#_####_##__#_##_###__#_____#__________________________
____________________________#__###__#_##____#__#_###__#______###_##_#__#__##________________________
___________________________###___#_____#_##_#_##__##__#####_####__####_##___#_______________________
__________________________#__###__#_#_#__#_###_#_#_##______##___#_#_#____#___#______________________
_________________________###___#__##_###__##_#___##_______####_####___#______#______________________
________________________#__###__#_#__#___##__###########_#__####__#____#____#_______________________
_______________________###___#__##______#_####__##__#########__#__##____#__##_______________________
______________________#__###__#_#___##__#_##___##_##_###_###___#__#_##__####_#______________________
_____________________###___#__##___#__#_######_##_#_##_#_#____###_###___##___#______________________
____________________#__###__#_#___#_____#####_#_#####_____#_#__##_#____##___#_______________________
___________________###___#__##____#_____#_##_#####_##__#_#___#__#__##_#__#__#_______________________
__________________#__###__#_#_____#____#___####_#__#####_##___##########___##_______________________
_________________###___#__##______#_##___##___#__#___####__#___##_####_##___________________________
________________#__###__#_#________#####_#__##___##_#___#____#_#__#__#__#_#_________________________
_______________###___#__##__________##__##_#_#_#____##_##_#_#_##__#__##__##_________________________
______________#__###__#_#_________________#__#____#_########_#_#_##__####_#_________________________
_____________###___#__##__________________#__#___#_______##_##___#__#__##_#_________________________
____________#__###__#_#____________________#__#__#______#__##__##___##_####_________________________
___________###___#__##______________________##___#_______##__##____#___#_###________________________
__________#__###__#_#____________________________#_##__####____####_###_####________________________
_________###___#__##______________________________##__####____##__#_##_#_#__#_______________________
________#__###__#_#________________________________##____##____##_###_##_#####______________________
_______###___#__##________________________________________________#_##_#__####______________________
______#__###__#_#_____________________________________________________##_##_##______________________
_____###___#__##______________________________________________________##____________________________
____#__###__#_#_____________________________________________________#_##__####_#____________________
___###___#__##_____________________________________________________#__#_###__###____________________
__#__###__#_#______________________________________________________#_##_#__#__#_____________________
_###___#__##________________________________________________________##______##______________________
#__###__#_#__________________________________________________________##_____________________________
_###_#__##__________________________________________________________________________________________
#_#_#_#_#___________________________________________________________________________________________
_####_##____________________________________________________________________________________________
_#_##_#_____________________________________________________________________________________________
__####______________________________________________________________________________________________
___##_______________________________________________________________________________________________
</pre>
 
=={{header|Euphoria}}==
Anonymous user