0% found this document useful (0 votes)
28 views2 pages

Norman Thomson Functional Programming With APL

The document discusses functional programming in APL2. It describes how functions can be treated as values and combined using operators. It provides examples of operators like else and until, and how they can combine functions. The document also discusses functional programming concepts like pure functions and how APL2 allows for unlimited scope in functional programming through user-defined operators.

Uploaded by

alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Norman Thomson Functional Programming With APL

The document discusses functional programming in APL2. It describes how functions can be treated as values and combined using operators. It provides examples of operators like else and until, and how they can combine functions. The document also discusses functional programming concepts like pure functions and how APL2 allows for unlimited scope in functional programming through user-defined operators.

Uploaded by

alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

although you can write an application to allow your m c n t s is m a d e u n t i l a t s o m e f i n a l s t a g e e x e c u t i o n is

user to highlight text blocks with the mouse, if you demanded. The essence of functional programming
want to do this w h e n using the function editor it's is t h a t f u n c t i o n s do n o t m e r e l y r e t u r n v a l u e s , func-
back to the Control-End key combination. Another t i o n s also a r e v a l u e s , so t h a t f ( x ) - - w h i c h m e a n s t h e
example is the Copy m e n u c o m m a n d which copies r e s u l t o f a p p l y i n g f to x - - i s q u i t e d i f f e r e n t f r o m f
to the Clipboard with Windows, but from wherever itself. T h e i d e a o f a f u n c t i o n i t s e l f b e i n g a v a l u e is
tagged lines are held in the API.JW function editor. r e f l e c t e d in A P L 2 o p e r a t o r s , a n d t h e p r e s e n c e o f
s a y i n n e r p r o d u c t in t h e l a n g u a g e is a c l e a r
a c k n o w l e d g m e n t t h a t it is p o s s i b l e t o p e r f o r m t h e
The Future f u n c t i o n a l g e b r a f.g r e g a r d l e s s o f a n y d a t a to w h i c h
D e s p i t e all o f t h e s e f r u s t r a t i o n s , A P L / W c o n t i n - t h i s f u n c t i o n c o m b i n a t i o n m i g h t u l t i m a t e l y be
u e s to be m y A P L o f p r e f e r e n c e . I c a n s y m p a t h i s e applied. S i n c e A P L 2 a l l o w s o p e r a t o r s to be user-de-
w i t h t h e d e c i s i o n s t h a t D y a d i c a r e h a v i n g to m a k e fined, t h e s c o p e for f u n c t i o n a l p r o g r a m m i n g in t h i s
in o r d e r to b r i n g a u s e a b l e p r o d u c t t o m a r k e t ; a n d s e n s e is u n l i m i t e d .
j u s t h o p e t h a t t h e r e s u l t i n g r e v e n u e s t r e a m is g o i n g As a s i m p l e i l l u s t r a t i o n h e r e is a n e x a m p l e o f
to be l a r g e e n o u g h to p e r m i t t h e s h o r t c o m i n g s to t w o o p e r a t i o n s w h i c h a r e i d e n t i c a l in t h e i r p r o g r a m
be a d d r e s s e d . As t h o s e o f y o u w h o w e r e p r e s e n t a t bodies, but which are quite different at a functional
D y a d i c ' s A P L 9 3 V e n d o r F o r u m will k n o w . t h e com- p r o g r a m m i n g level.
p a n y h a s s o m e v e r y a t t r a c t i v e p l a n s for t h e f u t u r e .
W e n o w h a v e t h r e e s i g n i f i c a n t p l a y e r s in t h e Z÷L MULl R Z÷(L MUL2)R
" A P L for W i n d o w s " a r e n a , all t a k i n g t h e i r d i f f e r e n t Z÷LxR Z÷LxR
stances. I h a v e n ' t b e e n a b l e to t a k e a n y s e r i o u s M U L l has two arguments, M U L 2 has only one.
look at Manugistics most recent versions of 2 M U L 2 3 means apply f = 2 MUL2--ca]I this the
APL*PLUS II, a n d I d o n ' t r e l i s h t h e a m o u n t o f " d o u b l e " f u n c t i o n - - t o a r g u m e n t 3 to r e t u r n 6. I t
s t u d y t h a t s e e m s to be n e c e s s a r y to c o m e to g r i p s is m e a n i n g f u l to t a l k a b o u t f 2 (3), w h i c h h a s t h e
w i t h t h e i r i n t e r f a c e ( a l t h o u g h I ' d h a p p i l y be p r o v e d v a l u e 1 2-
mistaken). I've looked briefly at ISI's APLIWIN, D y a d i c o p e r a t o r s c o m b i n e f u n c t i o n s in t h e s a m e
and see a product which combines technical virtue way that dyadic functions combine data. For exam-
with an individually-justifiable price (and noted that ple e l s e is a r e a d i l y c o n c e i v a b l e f u n c t i o n o f func-
D D E b e t w e e n A P L I W I N a n d A c c e s s a p p e a r s to t i o n s for w h i c h e l s e ~ g ) m e a n s "f, or i f n o t E-"
w o r k j u s t fine). A P L 2 for O8/2 b e c k o n s f r o m t h e T h e c o n d i t i o n s in e x e c u t i o n u n d e r w h i c h e i t h e r f or
wings to take us into that particular arena. g operates are a matter of data, not of function
O n e of the things that was most rewarding a l g e b r a . T h e l a t t e r is c o n c e r n e d o n l y w i t h d e f i n i n g
about A P L 9 3 was the very positive feeling which a f u n c t i o n w h i c h c o m b i n e s f a n d E, a n d in w h i c h f
people were beginning to be able to generate about is d o m i n a n t in s o m e d a t a - d r i v e n s e n s e . I m p l e m e n t a -
the future for APL; it was more than wishful think- tion of ELSE begins with two program body lines
ing. To paraphrase what another vendor said not
so long ago--we n o w have some excellent A P L Z+(P ELSE Q )R
development tools; the next step is fox us to start Z÷P R
building great applications with them. m Z÷Q R
both of which must be present but only one of
which is selected depending on data. At the second
stage of development a predicate L might be intro-
Functional Programming with APL2 duced to settle the issue of P or Q:
Z÷L(P ELSE Q )R
÷L 1 IF~L
Norman Thomson ÷0 Z÷P R
Mail Point 17H
LI:Z÷Q R
I B M U K Ltd.
P O B o x 30, (X~3)( ÷ELSE+ )X+2
G r e e n e ck, 0.25
Scotland PAl5 6AP (X~3)( ÷ELSE+ )X÷3
Tel: +44-475-895165 3
E-Maih nthomson@GNKVM2. VNET. IBM. com
A m o r e i n t e r e s t i n g i l l u s t r a t i o n is in t h e field o f
Functional programming is a style of program- iterative solution of numerical equations where the
ming which recognises that functions per se obey o p e r a t o r U N T I L h a s a f u n d a m e n t a l r o l e in t h e r e l e -
mathematical principles, quite separately from their v a n t f u n c t i o n algebra_
contents. In its purest form no reference to argu-

APL Q~ote ~ a d 15
Z÷(P UNTIL Q)R F NEWTON -1.2
÷LI IF Q R -0.9999999999
÷0 Z÷(P UNTIL Q)P R F NEWTON 1
L1 :Z÷R 2.000000001
U N T I L is a c o n n e c t i n g o p e r a t i o n b e t w e e n f u n c t i o n s A n a l t e r n a t i v e root-finding a l g o r i t h m to N e w t o n -
in m u c h the same w a y as addi t i on or m u l t i p l i c a t i o n Raphson is t h e so-called S e c a n t M e t h o d . Again
a r e o p e r a t i o n s w h i c h c o m b i n e n u m b e r s in simple U N T I L act s as a c o n n e c t o r b e t w e e n a step f u n c t i o n
arithmetic. In in f or m al t e r m s t h e above o p e r a t o r and a stopping c r i t e r i o n .
definition says t h a t t h e f u n c t i o n c o m b i n a t i o n
P UNTIL Q (the d e r i v e d f u n c t i o n in APL2 t e r m s ) Z÷(P SECANT )START
is i n v a r i a n t u n d e r t h e o p e r a t i o n o f P, s u b j e c t to t h e Z÷÷(P SECSTEP UNTIL(P EQUAL) )START
p r e d i c a t e Q r e m a i n i n g false.
As an example c o n s i d e r t h e following simple Z÷(P SECSTEP)X;T
i t e r a t i v e a l g o r i t h m for e x t r a c t i n g a sqt~are root. Z÷TpX P OPPOSITE(-/(4pX)xT)÷-IT÷P"X
T h e q u a n t i t y E P S should be u n d e r s t o o d as defining
t h e d i f f e r e n c e b e t w e e n s u c c e s s i v e i t e r a t e s w h i c h is Z÷L(P OPPOSITE)R;T
c o n s i d e r e d small e n o u g h to stop the al gor i t h m . Z÷(¢( xP R)=xP"T)/T÷(÷L),R,I+L
Z÷START SQRT Y
Z÷(P EQUAL)X;T
Z÷(IMPROVE UNTIL NEAR )START
Z ÷ E P S > IP ÷ X
Z÷IMPROVE X Z÷NEAR X
F SECANT I 3
Z÷O.5xX+Y÷X Z ÷ E P S > IY-X*2
1.99999857
S Q R T can be thought of as defining the procesa of
In a p s e u d o - m a t h e m a t i c a l not at i on:
extracting a square root using the particular iter-
ation sequence whose step is defined by .improve, secant(f) = Until(secstep(f),equal(f))
and whose stopping criterion by near.
In order to describe another iteration process, Another iterative method for finding the roots of f
for example Newton-Raphson iteration, it is neces- consists o f r e - e x p r e s s i n g f ( x ) = 0 as x = g ( x ) and
sary only to identify the analogous step function t h e n c a r r y i n g o u t r e p e a t e d s u b s t i t u t i o n in the for-
and stopping criterion: mula:

Z÷( P N E W T O N ) S T A R T Xr + 1 = g(Xr)
Z÷(P NEWSTEP UNTIL(P EQUAL) )START T h e same f ( x ) is used as above, r e c o g n i z i n g t h a t
t h e r e are two possible forms for g(x), n a m e l y x 2 - 2
Z÷(P NEWSTEP)X Z÷(P EQUAL)X;T and ~/~ + 2 -
Z÷X-(P X)÷P DERIV X Z+EPS>IP X Z÷(P ITER)START
Z÷(P UNTIL(P THERE) )START
Z÷(P DERIV)X
Z÷( (P X + A X ) - P X)÷AX Z÷GI X Z÷(P THERE)X
Functional programming style leads to a clear sepa- Z÷-2+X*2 Z ÷ E P S > IX-P X
ration between process description, and process
instance, which is what is more usually called exe- Z÷G2 X
cution. Z ÷ ( X + 2 )* .5
As an illustration of the latter, let f(x) be the
f u n c t i o n 2 - - x ( x - 1). To solve f ( x ) = 0 define t h e G1 ITER 1 G2 ITER 1
APL2 f u n c t i o n F: -1 1.999990588
Z÷F X Thes~uct~e
Z÷2-XxX-I
iterative_method (f) =Until (step(f), stop (f))
and s e t
is v e r y general , and e m e r g e s from t a k i n g a func-
6X tional p r o g r a m m i n g v i e w p o i n t o f t h e problem, o r
0.00005 r a t h e r family of problems. It is hoped t h a t t h e
T h e n use the function F as the left; argument i l l u s t r a t i o n s in this art i cl e show t h a t a f u n c t i o n a l
of
N E W T O N to obtain roots: p r o g r a m m i n g a p p r o a c h to problem-solving c a n in
t he r i g h t c i r c u m s t a n c e s lead to v e r y s t r i k i n g uses
o f t h e APL2 o p e r a t o r facility. •

16 December 1993 - Volume 24, Number 2

You might also like