Let it snow

Let it snow

You might have seen John Scholes’ quietly famous implementation (in Dyalog APL) of Conway’s Game of Life. We’re going to explore just one of the array-language features in it, indexing one array with another.

You can experiment with the expressions at tryapl.org. To animate them you need a Dyalog interpreter, with its editor and delay function.

The principle seems simple enough.

      'abcdef'[1 3 4 6]

The hidden surprise is that the result of such an expression has the same shape as the array in the index brackets.

      2 2⍴1 3 4 6
1 3
4 6
      'abcdef'[2 2⍴1 3 4 6]


Snow Crash, the title of Neal Stephenson’s acclaimed debut novel, is a reference to the random visual ‘white noise’ on the screen of a completely crashed computer. Let’s make some snow.

      '⎕⌹'[?2 2⍴2]
      '⎕⌹'[?2 2⍴2]

Now let’s follow John’s example and animate it.

      ⎕ed 'pic'
      {} {pic∘←'⎕⌹'[?30 100⍴2]⊣⎕DL÷⍵ ⋄ ⍵} ⍣40 ⊢8


Five seconds of ‘snow crash’. Perhaps it would look more snowy with snowflakes – for which we’ll use the five-pointed APL star.

      {} {pic∘←' *'[?30 100⍴2]⊣⎕DL÷⍵ ⋄ ⍵} ⍣40 ⊢8


Nice. Except real snow doesn’t flicker like that. It falls. It drifts.

Park that thought. Raise the temperature.

Oh, westron wind, when wilt thou blow
That the small rain down may rain?
Christ, that my love were in my arms
And I in my bed again!

wrote Henry VIII. Here we go.

      {} {pic∘←' |'[?30 100⍴2]⊣⎕DL÷⍵ ⋄ ⍵} ⍣40 ⊢8


Uh. Wasn’t that wind supposed to be blowing from the west?

{} {pic∘←' \'[?30 100⍴2]⊣⎕DL÷⍵ ⋄ ⍵} ⍣40 ⊢8


Enough rain. Back to snowflakes. And fewer of them.

      pic←' *'[1+1=?30 100⍴20]


Flakes must fall.We need a skyfall algorithm. Drop the bottom row, prefix new flakes at the top:

      pic←{' *'[1+1=?100/20]⍪¯1 0↓⍵} pic


And some animation.

      next←{' *'[1+1=?100/20]⍪¯1 0↓⍵} 
      {} {pic∘←next ⍵⊣⎕DL÷8} ⍣40 ⊢pic

This snow falls straight down, which isn’t quite right. We expect a little wind. From the west, perhaps.

   (L T)←{' *'[1+⍵]}¨1=?((⍴⍵)-0 1)⍴¨20
   L,T⍪¯1 ¯1↓⍵
{} {pic∘←next ⍵⊣⎕DL÷8} ⍣200 ⊢pic

The snowfield is 3-D. Nearer flakes should be larger – and fewer – than distant ones.

   (L T)←{'⍟**∘∘∘...... '[⍵]}¨13⌊?(0 ¯1+⍴⍵)⍴¨100
   L,T⍪¯1 ¯1↓⍵
{} {pic∘←next ⍵⊣⎕DL÷8} ⍣200 ⊢pic


There we go. 25 seconds of simulated snowfall.

We’re missing some features. Turbulence produces a certain amount of random jiggling for the flakes. Distant flakes should fall more slowly than near flakes. We’ve reached the limit of our simple representation of a snowfield as 2-D character array. We might (in a later post) represent the flakes differently, and project their representations onto a visual plane.

Watch the whole thing on YouTube!

For now, a Merry Christmas and – let it snow!


Arrays, actually

Arrays, actually

What is it about the array languages? What does it mean to ‘think in arrays’? This blog aims to exhibit what is distinctive about the array languages descended from Iverson Notation, originally developed at Harvard in the 1950s.

We’re not trying here to teach you these languages. We suppose you already know a few languages and are curious about these. We aim to satisfy your curiosity and help you decide whether you want to learn and use array languages. We will explain what we think are the key concepts in the code we show.

The languages shown here are Dyalog APL, J and kdb. You can experiment with APL online at TryAPL.org.

Dan Baronet & Stephen Taylor