Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Small, cool examples of APL's power

250 views
Skip to first unread message

Aaron W. Hsu

unread,
Jan 27, 2012, 5:04:44 PM1/27/12
to
Does anyone have any cool, concise, but very impressive examples of APL's
capabilities?

--
Aaron W. Hsu | arc...@sacrideo.us | http://www.sacrideo.us
לֵ֤ב חֲכָמִים֙ בְּבֵ֣ית אֵ֔בֶל וְלֵ֥ב כְּסִילִ֖ים בְּבֵ֥ית שִׂמְחָֽה

markg

unread,
Jan 27, 2012, 5:44:09 PM1/27/12
to
On Jan 27, 2:04 pm, "Aaron W. Hsu" <arcf...@sacrideo.us> wrote:
> Does anyone have any cool, concise, but very impressive examples of APL's
> capabilities?

Impressive: the FinnAPL Idiom List and the IBM APL Idiom List. Very
impressive: Roy Sykes' Whizbang! columns in Quote Quad. I don't have
links at hand; consider these Google fodder.

..mark

Charles Richmond

unread,
Jan 27, 2012, 7:48:13 PM1/27/12
to
"Aaron W. Hsu" <arc...@sacrideo.us> wrote in message
news:op.v8rjl6bm0p3ku8@localhost...
> Does anyone have any cool, concise, but very impressive examples of APL's
> capabilities?
>

There is a one-line function written by Ken Iverson that computers the
determinant of a matrix. It was published in the August 1977 issue of BYTE
magazine. I am *not* well enough versed in the APL to ASCII translation...
to be able to post the function here.

--
+<><><><><><><><><><><><><><><><><><><>+
| Charles Richmond nume...@aquaporin4.com |
+<><><><><><><><><><><><><><><><><><><>+

Don Kelly

unread,
Jan 27, 2012, 9:36:55 PM1/27/12
to
On 27/01/2012 2:04 PM, Aaron W. Hsu wrote:
> Does anyone have any cool, concise, but very impressive examples of
> APL's capabilities?
>
what about
B is A[gradeup A] for a sorted list?
Compare this to a program to do this in C (+ whatever)

Another is a one liner to calculate sin x from the series to an
arbitrary number of terms out to order n
As I recall it goes something like this.
Y is -/ (X*S) divided by ! S is (N rho 1 0)/i,N


A trouble with many such one-liners is that they become too complex when
one is learning whereas the simple sorts and things like calculation of
a mean which are easily grasped- and there is no big programming
overhead in defining variable types and setting up loops. I'm running
into this as a J beginner


--
Don Kelly
cross out to reply

Phil Last

unread,
Jan 28, 2012, 7:01:00 AM1/28/12
to
Forwarded from: Kai Jaeger

> Impressive: the FinnAPL Idiom List and the IBM APL Idiom List.

The FinnAPL list is outdated; both are not exactly brilliantly
documented. Many expressions on the APL2 list won't work elsewhere.

Anyway, you can find the APL2-related stuff at:

http://aplwiki.com/PhraseBook/ToDo/Apl2Idioms

and the FinnAPL stuff:

http://aplwiki.com/FinnAplIdiomLibrary

Recently a project started on the APL wiki; goal is to put up a
collection of one-liners which are up-to-date and well-documented:

http://aplwiki.com/CategoryPhrasesAll

These are vendor independent. However,the large number of volunteers
makes this a slow process... ;)

But APL is not about one-liners. In fact we should provide one-liners
only if they come naturally.

The arguably best source for amazing stuff you can do with APL is
probably:

http://dfns.dyalog.com/

This however is not only pure Dyalog APL, all the stuff is written in
D (or "direct function/operators), a functional branch in Dyalog APL.
It has been written by one of APL's grand masters, John Scholes, who
was also behind the most amazing APL-related video I've seen so far:

http://www.youtube.com/watch?v=a9xAKttWgP4

You can even calculate the Life, the Universe and Everything in APL:

MeaningOfLife←{+.×/⍎⊖⍕⊃¨⊂|⌊-*+○⌈∨.∧/>\</×÷!⌽⍉⌹~⍴⍋⍒,⍟?⍳''≡1↑0⍴∊⍵}

For details see

http://aplwiki.com/MeaningOfLife

Roger Hui

unread,
Jan 30, 2012, 7:00:29 PM1/30/12
to
> Does anyone have any cool, concise, but very impressive examples of APL's
> capabilities?

I have a recent example from the "88 Hats" problem.
http://www.jsoftware.com/papers/88hats.htm . The line is
(⍳m) g gi (∘.≠⍨⍳m)g.×⍵
This takes some explanation.

g is the (scalar) operation of a finite abelian group of order m. The
group elements are renumbered such that 0 is the identity element.

gi is the inverse of g.

(∘.≠⍨⍳m) is the logical negation of the identity matrix of order m.

The line (⍳m) g gi (∘.≠⍨⍳m)g.×⍵ computes, for each element i of ⍳m,
the element i g gi j, where j←g/(⍳m)~i, the g-reduction of all
elements of ⍳m excepting i. The inner product g.× is neat because the
× is ordinary arithmetic multiplication whereas g is the operation of
an arbitrary finite group. The line in APL compares favorably with
its statement in J,
(⍳m) g gi (∘.≠⍨⍳m)g.×⍵
(i.m) g gi 1 g/\.y
which uses the "outfix" g/\.

The expressions arise in a solution to the 88 hats problem (URL
above), previously solved by John Randall, J user and professor of
mathematics and by Ronald Chan, Ph.D. candidate in engineering and
winner of the Dyalog programming contest in 2009.

88 people stand in a circle, each having a hat with a number from 0 to
87 written on it. Everyone can see the numbers on other people’s hats
but can not see his own number. They simultaneously write a number on
a piece of paper and give it to the judge. If at least one of them
wrote a number that is on his own hat then everyone wins, otherwise
everyone loses. What strategy should they use to guarantee victory?

(Numbers on the hats do not have to be all different. People can not
exchange any information during the procedure but can agree on some
strategy beforehand.)



On Jan 27, 2:04 pm, "Aaron W. Hsu" <arcf...@sacrideo.us> wrote:
> Does anyone have any cool, concise, but very impressive examples of APL's
> capabilities?
>
> --
> Aaron W. Hsu | arcf...@sacrideo.us |http://www.sacrideo.us

Roger Hui

unread,
Jan 30, 2012, 7:42:36 PM1/30/12
to
> Does anyone have any cool, concise, but very impressive examples of APL's
> capabilities?

Long time APL/J users (myself included) sometimes forget that many
simple expressions are impressive even though they are "old hat" and
likely taught on the first day of an introductory APL course. For
example:

x+y - x plus y where x and y can be scalar, vector, matrix, ...
x×y - x times y where x and y can be scalar, vector, matrix, ...
x⌈y - x max y where x and y can be scalar, vector, matrix, ...

+⌿x - the sum of x
×⌿x - the product of x
⌈⌿x - the maximum of x

⍋ages - the indices that would put ages in ascending order
ages[⍋ages] - sort ages
names[⍋ages;] - names sorted by ages

If you don't think these are impressive, try writing them in any other
programming language.



On Jan 27, 2:04 pm, "Aaron W. Hsu" <arcf...@sacrideo.us> wrote:
> Does anyone have any cool, concise, but very impressive examples of APL's
> capabilities?
>
> --
> Aaron W. Hsu | arcf...@sacrideo.us |http://www.sacrideo.us

Aaron W. Hsu

unread,
Jan 30, 2012, 7:56:27 PM1/30/12
to
Hey Roger:

While I do think that in and of themselves, many of these examples are
rather impressive to some degree, I do find that among PL researchers this
is not nearly as impressive, since many of them work on languages that
have similar things. For example, many would say that such things are not
so bad in other languages like Haskell or Scheme. If you know anything
about these other languages and can compare for me what you think, I would
appreciate it. In particular, I'm quite interesting in hearing what other
APLers think are the really differentiating features of APL compared to
other frontier PL languages. Granted, of course, APL is actually a mature,
very delivered product, while many research languages are quite
unfinished, but I do not think that we can say this about languages like
ArBB, Haskell, Scheme, ML, or Matlab. Here are some examples of those when
encoded into Scheme:

(map + x y) ; X and Y must be lists
(map * x y) ; X and Y must be lists
(map max x y) ; X and Y must be lists

(fold-left + 0 x) ; X must be a list
(fold-left * 1 x) ; X must be a list
(fold-left max -inf.0 x) ; X must be a list

(list-sort < ages) ; AGES must be a list
(map car (list-sort (lambda (x y) (< (cdr x) (cdr y))) (map cons names
ages)))
; NAMES and AGES must be lists

To some extent these are not as flexible as the APL primitives, and I
think that's a key difference, from my perspective. These primitives are a
lot nicer because they are much more generic than some of the equivalent
expressions in other languages, and as such, require less mental overhead
to work with. On the other hand, some might argue that this isn't such a
big deal.

On Mon, 30 Jan 2012 19:42:36 -0500, Roger Hui <rogerhu...@gmail.com>
wrote:

> Long time APL/J users (myself included) sometimes forget that many
> simple expressions are impressive even though they are "old hat" and
> likely taught on the first day of an introductory APL course. For
> example:
>
> x+y - x plus y where x and y can be scalar, vector, matrix, ...
> x×y - x times y where x and y can be scalar, vector, matrix, ...
> x⌈y - x max y where x and y can be scalar, vector, matrix, ...
>
> +⌿x - the sum of x
> ×⌿x - the product of x
> ⌈⌿x - the maximum of x
>
> ⍋ages - the indices that would put ages in ascending order
> ages[⍋ages] - sort ages
> names[⍋ages;] - names sorted by ages
>
> If you don't think these are impressive, try writing them in any other
> programming language.


--
Aaron W. Hsu | arc...@sacrideo.us | http://www.sacrideo.us

Roger Hui

unread,
Jan 30, 2012, 8:44:27 PM1/30/12
to
A couple of the phrases in your msg are crucial:

Zero, "X and Y must be lists". The APL expressions work for arrays of
any rank.

One, "require less mental overhead". You can buy a more powerful
computer, but you can't buy a more powerful head!

Another couple of further examples which are instances of the "old
hat" ideas but uses other functions.

0. Simulate the number of heads in n coin flips:
?10⍴2
1 0 1 1 0 1 1 0 1 0
n←1e6
+/?n⍴2
500953
+/?n⍴2
500682
+/?n⍴2
499842

1. Simulate n trials of the Monty Hall problem.
http://www.jsoftware.com/jwiki/Essays/The_Monty_Hall_Problem

The object of the game is to win a car.

Two goats and a car are hidden behind 3 doors, one item per door. You
choose a door. The game-master (Monty Hall), who knows what's behind
the doors, opens one of the other doors, revealing a goat, and offers
you the opportunity to change your choice of doors. Your chosen door
is then opened and you get what is revealed.

Should you stick or change?

MontyHall←{
c←?⍵⍴3 ⍝ where the car is hidden
i←?⍵⍴3 ⍝ your original choice of door
~⍺:c,[0.5]i
j←(c×i≠c)+(3|1+i+?⍵⍴2)×i=c ⍝ your changed choice
c,[0.5]j
}

The result of ⍺ MontyHall ⍵ is a (⍵,2) matrix where column 0 is where
the car is hidden and column 1 is your original (if ⍺=0) choice of
door or your final (if ⍺=1) choice of door. The number of times you
win a car is therefore +/=/⍺ MontyHall ⍵.

0 MontyHall 10
2 0
2 1
1 0
0 2
2 2
1 0
0 0
0 2
1 2
0 0
+/=/0 MontyHall 1e6
333361
+/=/0 MontyHall 1e6
332578


1 MontyHall 10
1 1
1 0
1 1
1 1
2 2
1 0
1 2
2 2
0 0
2 0
+/=/1 MontyHall 1e6
667164
+/=/1 MontyHall 1e6
666692

I am also interested in how you'd do the g.× inner product in other
languages.
> On Mon, 30 Jan 2012 19:42:36 -0500, Roger Hui <rogerhui.can...@gmail.com>
> wrote:
>
>
>
>
>
>
>
>
>
> > Long time APL/J users (myself included) sometimes forget that many
> > simple expressions are impressive even though they are "old hat" and
> > likely taught on the first day of an introductory APL course.  For
> > example:
>
> > x+y - x plus y where x and y can be scalar, vector, matrix, ...
> > x×y - x times y where x and y can be scalar, vector, matrix, ...
> > x⌈y - x max y where x and y can be scalar, vector, matrix, ...
>
> > +⌿x - the sum of x
> > ×⌿x - the product of x
> > ⌈⌿x - the maximum of x
>
> > ⍋ages - the indices that would put ages in ascending order
> > ages[⍋ages] - sort ages
> > names[⍋ages;] - names sorted by ages
>
> > If you don't think these are impressive, try writing them in any other
> > programming language.
>
> --
> Aaron W. Hsu | arcf...@sacrideo.us |http://www.sacrideo.us

Bakul Shah

unread,
Jan 31, 2012, 2:34:34 AM1/31/12
to
On 1/30/12 4:00 PM, Roger Hui wrote:
>
> 88 people stand in a circle, each having a hat with a number from 0 to
> 87 written on it. Everyone can see the numbers on other people’s hats
> but can not see his own number. They simultaneously write a number on
> a piece of paper and give it to the judge. If at least one of them
> wrote a number that is on his own hat then everyone wins, otherwise
> everyone loses. What strategy should they use to guarantee victory?
>
> (Numbers on the hats do not have to be all different. People can not
> exchange any information during the procedure but can agree on some
> strategy beforehand.)

They agree to write the same number?

Roger Hui

unread,
Jan 31, 2012, 8:04:49 AM1/31/12
to
> They agree to write the same number?

Let's say they all agree to write 0. But the hats all had 1.
0 new messages