One-dimensional cellular automata: Difference between revisions

No edit summary
Line 3,103:
(nil nil 1 nil nil)
(nil nil nil nil nil)</pre>
 
===More sophisticated===
<lang python>def (uca l) # new datatype: unidim CA
(tag uca (list l len.l))
 
def (len l) :case (isa uca l) # how to compute its length
rep.l.1
 
defcoerce uca list # converting it to list
(fn(_) rep._.0)
 
def (pr l) :case (isa uca l) # how to print it
each x l
pr (if x "#" "_")
 
# (l i) returns ith cell when l is a uca, and nil when i is out-of-bounds
defcall uca (l i)
if (0 <= i < len.l)
rep.l.0.i
 
def (gens n l)
prn l
repeat n
zap! nextgen l
prn l
 
def (nextgen l)
uca+collect+for i 0 (i < len.l) ++i
yield (next (l i-1) l.i (l i+1))
 
# next state of b, given neighbors a and c
def (next a b c)
if (and a c) not.b
(or a c) b</lang>
 
Output is prettier now:
 
<pre>ready! type in an expression, then hit enter twice. ctrl-d exits.
gens 10 (uca '(nil 1 1 1 nil 1 1 nil 1 nil 1 nil 1 nil 1 nil nil 1 nil nil))
 
_###_##_#_#_#_#__#__
_#_#####_#_#_#______
__##___##_#_#_______
__##___###_#________
__##___#_##_________
__##____###_________
__##____#_#_________
__##_____#__________
__##________________
__##________________
__##________________</pre>
 
=={{header|XPL0}}==
143

edits