6
ac1235
7y

Today's achievement: wrote game of life in K3.

neigh:{(+/,/-1 0 1!'\:/:-1 0 1!\:x)-x}
life:{(x*2=neigh x)|3=neigh x}

Comments
  • 0
    What is K3?
  • 0
    Don't know if this is a sarcastic rant or a dark spell, but take my ++ apl overlord.
  • 1
    Classic Conway rules? Nice.
  • 1
  • 0
    @eisterman
    <language name>[<version>]

    example: Python2, K3, Java8
  • 0
    @bioDan neither nor. It's just some code.
  • 0
    @ac1235
    I was jk, would you kindly explain what it does to a non-apl programmer?
  • 1
    @bioDan sure. But you have to wait a bit, because I can't type on my mobile :D
  • 0
    Let's just start with the broad picture:
    We have two little functions: neigh and life.

    'neigh' receives a matrix (list of lists) of bools, 0 for dead cell and 1 for living cell respectively. It returns an integer matrix, where each cell is now represented by the number of neighbors (0-8).

    'life' is where the magic happens: we receive a bool matrix, a generation of game of life, and return the next generation, also as a bool matrix.

    > life:{(x&2=neigh x)|3=neigh x}

    The rules are simple: A cell with 3 neighbors will live on (3=neigh x).
    If a cell is already alive (x) and (&) has 2 neighbors (2=neigh x) it will also survive.

    'x' is our bool matrix we get as an argument. K conveniently allows us to not name our argument explicitly, but we could have called it 'board' or 'grid' or something.

    If you want to, I can also explain 'neigh'.
    Feel free to ask a question :)
  • 0
    @bioDan The "magic" is that K loops for us.

    Instead of writing "for row in x: for cell in row: ..." we can just use "x" in an element context and K does the looping for us.

    This is basically the main property distinguishing APL, K, ... from other languages.

    For example, list of 5 numebers:
    > !5
    0 1 2 3 4

    Now we want to add 1 to each, but since K does the for-eachy stuff for us, we can just write:

    > 1+ !5
    1 2 3 4 5
  • 0
    @ac1235 I don't know what language is K :/
  • 0
    @eisterman You can find a little description above.
  • 0
    @ac1235 thanks! Although except from shorter syntax, i can't see the advantage from lets say (in Ruby which in return has better readability):
    (0...5).each { |num| num + 1}
  • 1
    @bioDan It might be more readable to implement 1+each in Ruby, but writing game of life in it will take you longer.
    It is not about short code, it's about a short thought process: I can write something like this in seconds and APLers do have an easy time reading it.
    The main advantage IMO is composability: since we don't use loops or recursion in K, we basically create a bunch of reusable functions without writing closed systems like loops or recursive functions or alike.
    This allows for very high code reusability and, in turn, less work.
  • 1
    @ac1235 good answer, thanks
Add Comment