Array thinking [was Re: Jforum: Newby Problem!]
Author Message
Array thinking [was Re: Jforum: Newby Problem!]

(Thanks to Mr. Donovan for providing a good starting point for
elucidating how we might get from scalar to array
formulations.)

of
addressing the original problem but I'd like to point out how
the code presented initially exemplifies a (common) non-array
approach to the problem.  This is also a response to a message
posted on the APL list 4 months ago (excerpt at bottom of this
posting) about array-oriented solutions to classical problems.

Brian's solution, except perhaps for the last 2 lines, is
array-oriented.  The original question contained code of a
form that is all-too-familiar - you see it all the time when

Code like this (from the original query):

Quote:
> c1 =: +/ ('C' = , h1) NB. count Clubs in hand 1 etc.
> d1 =: +/ ('D' = , h1)
> h1 =: +/ ('H' = , h1)
> s1 =: +/ ('S' = , h1)

> c2 =: +/ ('C' = , h2)
> d2 =: +/ ('D' = , h2)
> h2 =: +/ ('H' = , h2)

...

(i.e. repetitious, functionally equivalent lines resulting
in scalars) signals that an array has been "strung out"
into scalar operations.  Brian's rewrite goes a long way to
restoring the array nature of the problem, e.g.:

NB. Deal hands for bridge
card0=: 3 : 0
s=: 'CDHS'
r=: 'A23456789TJQK'
s0=: /:"1~ 4 13 \$ 52 ?. 52      NB. the four hands, sorted
s1=:(<.s0%13){s                 NB. suits
v=:(13|s0){r                    NB. card values NB. Create a 4
4 13
NB. array for Jacks, Queens,
etc.,
NB. assign values (J=1, Q=2,
etc.),
NB. sum the four arrays, then
sum the hands
points=:+/"1+/1 2 3 4*(v='J'),(v='Q'),(v='K'),:v='A'
sdist=:|:+/"1(s1='C'),(s1='D'),(s1='H'),:s1='S'        NB. The
number of
NB. clubs, diamonds, hearts and
NB. in each hand
)

The last 2 lines, however, are still overly scalar as signaled
by the repetition of scalar operations (e.g.
(v='J'),(v='Q'),...)

A little playing around gives us

points=: +/|:+/1 2 3 4*'JQKA'=/v     NB. Points for Jacks,
Queens, etc.
sdist=: |:+/|:s =/"1 s1              NB. Distribution of
suits in "s" order.

The array versions are more concise and easily extensible than
the scalar ones.  With fluency in J, they may even be more
easily comprehended.

(Excerpts follow)

--- Question from several months ago ---

Quote:
> Date: Thu, 2 Aug 2001 14:34:50 -0300
> Subject: Array Oriented interpretation of Classical
Algorithms

> I am a green horn APL/J learner wanna be.
> I realised as I started working with the language that loops
tend to
> be a big no no and in APL there wasn't much by way of
branching... I
> was wondering if there were any pointers/papers etc available
to
> direct one in 'array oriented thinking' such that given a
classical
> algorithm containing loops and conditionals, an array
oriented
> interpretation could be given for it.
> In short, how to think of loops and branching in terms of

arrays.

--- Question to which Brian replied ---

23:32:52):

- Show quoted text -

Quote:
> Hi!

> Could someone please tell me why I get the
> following error...

> cards ''

> |domain error: cards
> |   sdist2=:c2,d2,h2    ,s2

> ...when I run the cards function from this script (the line
> in error is right near the end) Also value s1, the number
> of Spades held in hand number 1, is returned as zero although
> hand 1 has been dealt spades!!!

> Everything else seems to be working OK :( Thanks in advance,
> (FWIW this is just a test program as I have just gotten hold
of
> the language. It deals 52 cards bridge-style then calculates
the
> high-cards points and suit distribution)

> s=. 'CDHS'
> r=. 'A23456789TJQK' s0=. (i.4) { (4 13 \$ (52 ?. 52)) h1 =: 1

39 \$ ;(( <.(/: ~ 0 { s0) % 13) { s) ,. (13 | (/: ~ 0 { s0)) r
Quote:
> ,. ' '
> h2 =. 1 39 \$ ;(( <.(/: ~ 1 { s0) % 13) { s) ,. (13 | (/: ~ 1
{ s0)) {r
> ,. '
> '
> h3 =. 1 39 \$ ;(( <.(/: ~ 2 { s0) % 13) { s) ,. (13 | (/: ~ 2
{ s0)) {r
> ,. '
> '
> h4 =. 1 39 \$ ;(( <.(/: ~ 3 { s0) % 13) { s) ,. (13 | (/: ~ 3
{ s0)) {r
> ,. '
> ' h =: h1,h2,h3,h4 NB. Count points in each hand - Ace=4,

King=3, Queen=2, Jack=1 p1 =: (4 * +/ ('A' = , h1))+(3 * +/
('K' = , h1))+(2 * +/ ('Q' = ,
Quote:
> h1))+(1 *
> +/ ('J' = , h1))
> p2 =: (4 * +/ ('A' = , h2))+(3 * +/ ('K' = , h2))+(2 * +/
('Q' = ,
> h2))+(1 *
> +/ ('J' = , h2))
> p3 =: (4 * +/ ('A' = , h3))+(3 * +/ ('K' = , h3))+(2 * +/
('Q' = ,
> h3))+(1 *
> +/ ('J' = , h3))
> p4 =: (4 * +/ ('A' = , h4))+(3 * +/ ('K' = , h4))+(2 * +/
('Q' = ,
> h4))+(1 *
> +/ ('J' = , h4)) pdist =: p1,p2,p3,p4  NB. Points

distribution c1 =: +/ ('C' = , h1) NB. count Clubs in hand 1
etc.

- Show quoted text -

Quote:
> d1 =: +/ ('D' = , h1)
> h1 =: +/ ('H' = , h1)
> s1 =: +/ ('S' = , h1)
> c2 =: +/ ('C' = , h2)
> d2 =: +/ ('D' = , h2)
> h2 =: +/ ('H' = , h2)
> s2 =: +/ ('S' = , h2)
> c3 =: +/ ('C' = , h3)
> d3 =: +/ ('D' = , h3)
> h3 =: +/ ('H' = , h3)
> s3 =: +/ ('S' = , h3)
> c4 =: +/ ('C' = , h4)
> d4 =: +/ ('D' = , h4)
> h4 =: +/ ('H' = , h4)
> s4 =: +/ ('S' = , h4)
> sdist1 =: c1,d1,h1,s1
> sdist2 =: c2,d2,h2,s2
> sdist3 =: c3,d3,h3,s3
> sdist4 =: c4,d4,h4,s4
> )

__________________________________________________
Do You Yahoo!?
Send your FREE holiday greetings online!
http://www.*-*-*.com/

Mon, 21 Jun 2004 01:27:34 GMT

 Page 1 of 1 [ 1 post ]

Relevant Pages