5 ms·
Conway's Game of Life in APL in Forth
- isaacfrond 4y agotl;dr - a 200 line implementation of Life together with a heavily annotated version of the same thing. Might get you into APL or scare you off, more likely.
- curiousgal 4y agoThis video is what should get you into APL https://youtu.be/_DTpQ4Kk2wA https://youtu.be/_DTpQ4Kk2wA
- mlajtos 4y agoI love that demo! I want something like that, but much more modern. I already have demo video ready – https://youtu.be/y5Tpp_y2TBk?t=18 https://youtu.be/y5Tpp_y2TBk?t=18
- andybak 4y agoWell. It's a 1 line implementation in APL. The 200 lines are there so that Forth can emulate enough of APL to execute the 1-liner.
- isaacfrond 4y agoI'm a sucker for GPT3. In his weekly column the famous IT philosopher explains issues in computation in a very thoughtful manner. Recently, Conway's Game of Life was implemented as a one-liner in APL executed in FORTH. APL is another weird-looking programming language. Implementing Conway's game of Life yet again is important because it is a fundamental example of a computation. In this case, the one-liner is important because it is an example of how to concisely express a computation. Conway's Game of Life is a cellular automaton. A cellular automaton is a discrete model of computation. It consists of a grid of cells, each of which can be in one of a finite number of states. The state of the automaton is updated in discrete time steps, according to a fixed rule. In Conway's Game of Life, the rule is that a cell is alive in the next time step if and only if it was alive in the previous time step and exactly two or three of its eight neighbors were also alive. If a cell is not alive in the previous time step, then it is alive in the next time step if and only if exactly three of its neighbors were alive. The one-liner in APL executes the Game of Life rule on a two-dimensional grid of cells. The input to the one-liner is the state of the grid at some time step, and the output is the state of the grid at the next time step. The one-liner is able to do this because it uses a clever trick. It treats the input grid as a one-dimensional array of cells, and it uses the fact that the Game of Life rule can be expressed as a matrix operation. The one-liner is: life←{↑1 ⍵∨.↑1 3 4⍵} This one-liner is an example of how to concisely express a computation. It is also an example of how to use a powerful programming language to express a computation in a simple and elegant way.
- Stratoscope 4y agoWhoever wrote that is much smarter than me!
- shagie 4y agoIt isn't so much a "the author of that one liner is way smarter" but rather "the language features of APL naturally fit this problem space."
- exikyut 4y agoI didn't notice it initially but (part of) the two top lines are italicized. Presumably this was the input prompt. Which is kind of cool to be able to look at and observe that it made that from just those specific 48 input words. Huh.
- tromp 4y agoThat looks like a butchered version of life ← {⊃1 ⍵ ∨.∧ 3 4 = +/ +⌿ ¯1 0 1 ∘.⊖ ¯1 0 1 ⌽¨ ⊂⍵} as shown at https://aplwiki.com/wiki/Conway%27s_Game_of_Life https://aplwiki.com/wiki/Conway%27s_Game_of_Life
- deleted 4y ago[deleted]
- dlsa 4y agoI might need to learn APL. It seems like I'm missing out on something. Lisp was a gift which helped with my C and Elixir. That was a mini-revelation in itself.
- beagle3 4y agoAPL, K or J ; I've learned APL, and was able to use it effectively - but things only really "clicked" when I learned K; And despite several attempts, J never did click for me -- not sure why, probably the associations I have for the ascii characters are too strong to break. But yes, highly recommend learning members of this family. It will help your C for sure as well, though possibly not in ways you'd expect. After using APL and K for a while, I realized 95% of "abstraction" features in C++ / C# and friends are useless; I now avoid C++; My C code became way shorter, way more efficient, way simpler -- but also much less orthodox.
- yakubin 4y agoWhat do you recommend to learn APL and K?
- dlsa 4y agoFrom https://en.wikipedia.org/wiki/K_%28programming_language%29 https://en.wikipedia.org/wiki/K_%28programming_language%29 Here's their example: The following expression sorts a list of strings by their lengths: x@>#:'x Ooooooookay. Personally, I recommend a lot of alcohol. This will be a very wild and geeky ride. Bring extra pizza.
- yakubin 4y agoSounds like a recipe for alcohol addiction and obesity. Otherwise maybe I'd try that. ;)
- beagle3 4y agoPersonally, I recommend less alcohol. Pronounced, this would be "x at grade-down of count of each x" - and indeed, in "q" (a syntactic sugar for K that uses words instead of symbols), this is almost how you would write it (rather: "x at downgrade count each x". It's basically algorithmic math notation: In math you say "b^2-4ac" rather than "b squared minus 4 times a times c". In K you say "x@>#:'x". It's not just a matter of symbol/syntax familiarity - there's also idioms, etc. But the "scariness" is similar to the "scariness" of a language like Japanese or Arabic, which uses different graphic elements, syntax, vocabulary, and idioms. You might not like it, but it's not because there's something weird or wrong about it -- it's just foreign.
- andyjohnson0 4y agoFascinating. And given that GoL is Turing complete, it should be theoretically possible to implement a Turing machine in the cellular automaton that could then run a copy of the Forth interpreter running the APL program running the CA.
- schwartzworld 4y agoThere is a really excellent podcast on Array programming languages. https://www.arraycast.com/ https://www.arraycast.com/
- jshaqaw 4y agoPlease make your Turing complete game of life execute arbitrary Lisp code and you will have rescued the holy grail of computer language hacker nerdom here.
- exikyut 4y agoHmmmmmm. Native port of SectorLISP (https://justine.lol/sectorlisp/ https://justine.lol/sectorlisp/) to the Quest for Tetris processor (https://codegolf.stackexchange.com/questions/11880/build-a-working-game-of-tetris-in-conways-game-of-life https://codegolf.stackexchange.com/questions/11880/build-a-w...), anybody? SectorLISP is x86-specific, so this wouldn't just be neatly mushing existing things together, but I think the result could be very interesting nonetheless. I'm tempted to note that SectorLISP's tiny size could make this a reasonable one-shot/nerdsnipe-sized weekend project :D
- transfire 4y agoA Beautiful bit of coding.