Current issue

Vol.26 No.4

Vol.26 No.4

Volumes

© 1984-2026
British APL Association
All rights reserved.

This is the historical archive of the British APL Association.

The current active Journal can be found at the Vector Site.

Registered contributors can author their own material for posting on this site. Go to Vector find out more.

Follow this link to find out more about the British APL Association.

archive/25/3

Volume 25, No.3

Letter

The ruler’s edge

Norman Thomson

It is a pleasure to be able to respond my old partner in education Ray Polivka [1] on the subject of programming rulers. Readers may like to compare the different style of approach which might be taken in J to this problem, which, to remind readers, is to write a program for a ruler of length y. with numbers and tick marks at intervals of x..

First pre-define an adverb index

   index=.1 : '(i.@$*x.)@]'

which identifies elements where a criterion verb such as ‘equals zero’ is met, for example:

   t
0 1 0 1
0 0 1 1
1 0 1 1
   (=&0)index t	                 NB. give indices of 0s
0 0 2 0
4 5 0 0
0 9 0 0

This can be used in conjunction with the adverb amend (}) to provide a general purpose replacement facility, for example:

   99(=&0)index}t                NB. replace 0s with 99
99  1 99 1
99 99  1 1
 1 99  1 1

This is used in the final line of the verb numbers, which supplies the formatted numeric parts of the ruler:

   numbers=.4 : 0
t=.x.*>:i.>.y.%x.                NB. multiples of x.up to y.
t=.|:10 10 #:t 	                 NB. digitise and transpose
t=.y.{."(1)x.":t                 NB. put in spaced char form
t=.(' '(=&'0')index}{.t),:{:t    NB. blank ldng 0s
)
   3 numbers 17
           1  1
  3  6  9  2  5

Adding the tick marks, both horizontally or vertically, is now straightforward :

   rulera=.4 : 0
t=.x. numbers y.
t,(y.$((<:x.)#0),1){'-^'         NB. add tick marks
)
   rulerc=.4 : 0
t=.x. numbers y.
(|:t),.(0=x.|>:i.y.){'|+'        NB. add tick marks
)
   3 rulera 17
           1  1
  3  6  9  2  5
--^--^--^--^--^--

   3 rulerc 11
  |
  |
 3+
  |
  |
 6+
  |
  |
 9+
  |
  |

References

  1. “The ruler’s edge revisited”, Ray Polivka, Vector Vol.23, No.4 https://britishaplassociation.co.uk/art10012030

 

script began 18:28:36
caching off
debug mode off
cache time 3600 sec
indmtime not found in cache
cached index is fresh
recompiling index.xml
index compiled in 0.1914 secs
read index
read issues/index.xml
identified 26 volumes, 101 issues
array (
  'id' => '10500560',
)
regenerated static HTML
article source is 'XHTML'
completed in 0.2187 secs