I am going to create a program that can generate strings from L-system grammars.
Original Astrid Lindenmayer L-system for modeling algae growth:
variables: AB
constants: none
axiom: A
rules: (A โ AB), (B โ A)
which produces:
iteration | resulting model
0 | A
1 | Ab
2 | ABA
3 | Abaab
4 | ABAABABA
5 | ABAABABAABAAB
which is naively implemented by me in J as follows:
algae =: 1&algae : (([: ; (('AB'"0)`('A'"0) @. ('AB' i. ]))&.>"0)^:[) "1 0 1 (i.6) ([;algae)"1 0 1 'A' โโโฌโโโโโโโโโโโโโโ โ0โA โ โโโผโโโโโโโโโโโโโโค โ1โAB โ โโโผโโโโโโโโโโโโโโค โ2โABA โ โโโผโโโโโโโโโโโโโโค โ3โABAAB โ โโโผโโโโโโโโโโโโโโค โ4โABAABABA โ โโโผโโโโโโโโโโโโโโค โ5โABAABABAABAABโ โโโดโโโโโโโโโโโโโโ
Step by step illustration:
('AB' i. ]) 'ABAAB' NB. determine indices of productions for each variable 0 1 0 0 1 'AB'"0`('A'"0)@.('AB' i. ])"0 'ABAAB' NB. apply corresponding productions AB A AB AB A 'AB'"0`('A'"0)@.('AB' i. ])&.>"0 'ABAAB' NB. the same &.> to avoid filling โโโโฌโโฌโโโฌโโโฌโโ โABโAโABโABโAโ โโโโดโโดโโโดโโโดโโ NB. finally ; and use ^: to iterate
By analogy, this is the result of the 4th iteration of the L-system that generates the Thue-Morse sequence
4 (([: ; (0 1"0)`(1 0"0)@.(0 1 i. ])&.>"0)^:[) 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0
This is the best I can do so far. I believe that the box-unpacking method is not enough here. This is the first time I've missed linked lists in J - it's much harder to encode grammars without them.
What I really think about is: a) building a list of gerunds of those functions that build the final string (in my examples, these functions are constants of type 'AB'"0 , but in case the tree modeling functions are tortoise graphic commands ) and calling ( `:6 ) it,
or something that I can encode:
b) constructing a string of a legal J-sentence that builds the final string and does ( ". ) this.
But I'm not sure if these programs are effective.
- Can you show me a better approach?
Any hints, as well as comments about a) and b) are highly appreciated!