Arc Forumnew | comments | leaders | submitlogin
2 points by rntz 5375 days ago | link | parent

Have you actually run this code? It seems to have a lot of bugs. Also, this is just a style issue, but you seem to be indenting 8 spaces. Lisp convention is to put indents at 2 spaces, because lisp code tends to nest much more than code in C-like languages.

  (def alive? (c) (isa c.state 1))
  (def dead? (c) (isa c.state 0))
You want 'is, not 'isa. (is x y) tests if x and y are identical; (isa x y) tests if x's type is y; it's equivalent to (is (type x) y).

  (def doomed? (u) (is (len (keep [alive? _] u)) 0))
This is unnecessarily complex in two ways, and inefficient in a third. First, [alive? _] is just alive? itself. Second, the length of a list is zero if and only if that list is nil, and nil is itself a false value, so testing (is (len x) 0) when x is a list can be replaced with just (no x). Hence a simpler version would be:

  (def doomed (u) (no (keep alive? u)))
  ; with ssyntax we can simplify this even further:
  (def doomed (u) (~keep alive? u))
However, as mentioned by pg, this is still not the most efficient version of the function. If there are several alive cells in u, then (keep alive? u) will find all of them before terminating, when really we want it to exit with failure upon the first live cell it finds in u. Hence any one of (~mem alive? u), (~some alive? u) or (~find alive? u) will be more efficient (it doesn't really matter which you choose in this case).

As far as I can tell, your 'cindex function seems to be meant to take two integers and return a unique index. However, (cindex a b) == b + b*(a-1) == b + ba - b = ab. This obviously collides, for example, with (cindex b a).

You seem to use infix dot for accessing the fields of a cell a lot. This won't work; or example "c.fellas" will expand to "(c fellas)", which will depend on the value that fellas happens to be bound to (if any). What you probably want is "c!fellas", which expands to (c 'fellas).

  (def aliveneighbours c (map [isalive _] c.neighbours))
  ; did you mean this?
  (def aliveneighbours (c) (keep alive? c!neighbours))


1 point by adm 5375 days ago | link

Thanks for comments. No I have not run the code yet(and not even compiled).

Just to understand it: you say "c!fellas" => (c 'fellas), will that fetch the 'fellas element of c?

-----

1 point by conanite 5375 days ago | link

If c is a table, it will return the value stored with the key 'fellas

But watch out, you have a lot of

  (each c u ...
, where u is universe; universe is a table. You probably want something like

  (each (pos cell) u ...
because that's how 'each works with tables.

In 'init-universe, you have

  (= (pos u) (create-cell i j))
(pos u) needs to be the other way around:

  (= (u pos) (create-cell i j))
and you can shorten it to

  (= u.pos (create-cell i j))

-----