I implemented some Rosetta Code examples, one is Conway's life which I am quite pleased with.
import "prelude.eg"
using System
using List
using IO
def boardsize = 5
def empty = [ X, Y -> 0 ]
def insert =
[ X, Y, BOARD ->
[ X0, Y0 -> if and (X0 == X) (Y0 == Y) then 1
else BOARD X0 Y0 ] ]
def coords =
let R = fromto 0 (boardsize-1) in
[ XX, YY -> map (\X -> map (\Y -> X Y) YY) XX ] R R
def printcell =
[ 0 -> print ". "
| _ -> print "* " ]
def printboard =
[ BOARD ->
let M = map [XX -> let _ = map [X Y -> printcell (BOARD X Y)] XX in print "\n" ]
coords in
nop ]
def count =
[ BOARD, X, Y ->
(BOARD (X-1) (Y-1)) + (BOARD (X) (Y-1)) + (BOARD (X+1) (Y-1)) +
(BOARD (X-1) Y) + (BOARD (X+1) Y) +
(BOARD (X-1) (Y+1)) + (BOARD (X) (Y+1)) + (BOARD (X+1) (Y+1)) ]
def next =
[ 0, N -> if N == 3 then 1 else 0
| _, N -> if or (N == 2) (N == 3) then 1 else 0 ]
def updateboard =
[ BOARD ->
let XX = map (\X Y -> X Y (BOARD X Y) (count BOARD X Y))
(flatten coords) in
let YY = map (\X Y C N -> X Y (next C N)) XX in
foldr [X Y 0, BOARD -> BOARD | X Y _, BOARD -> insert X Y BOARD ]
empty YY ]
def blinker =
(insert 1 2) @ (insert 2 2) @ (insert 3 2)
def main =
let GEN0 = blinker empty in
let GEN1 = updateboard GEN0 in
let GEN2 = updateboard GEN1 in
let _ = map [ G -> let _ = print "generation:\n" in printboard G ]
{GEN0, GEN1, GEN2} in
nop
No comments:
Post a Comment