Nicholas Spies wrote:
> To conceal some of the visual noise implicit in J, I (foolishly, 
> no doubt) tried to do the following:

Any sufficiently advanced communication is indistinguishable
from noise.  (ftp://ftp.santafe.edu/pub/moore/ajp.pdf)

> NB.  defining this was the whole point of my feeble efforts
> inc =: 4 : '((>:(x. get y.))(x.) set y.)'
> dec =: 4 : '((<:(x. get y.))(x.) set y.)'
...
> Admittedly, trying to do this may be just plain foolish, but 
> what's the REAL way to do this?

Any real problem begins with a statement of what problem you're
trying to solve.  If you don't do this, it can be very difficult
to know if you've solved the right problem.

I'm going to guess you want to "add 1 or subtract 1 from arbitrary 
numbers in a sequence."

In J, if I have a numeric sequence
   0 0 0 0 0
And I want to add 1 to the second bit, I'd use
   0 0 0 0 0 + 0 1 0 0 0

Enough said?

Maybe not...

This doesn't match what you have above, so I'll add a
requirement, so that I match.  (In real life, it's usually
a good idea to eliminate all unnecessary requirements --
if they're unnecessary, they're not really requirements.
But we're just playing around here.)  So my new requirement
is: "I specify which locations to modify using indices."

Given a sequence like 0 0 0 0 0, one way of getting the
above bit vector is to use e. -- for example:

   1  e.~i.# 0 0 0 0 0
0 1 0 0 0 

Now I have several ways of defining inc in terms of a left arg
which contains indices and a right arg which contains a value
to be modified.  I don't use J504 any more, (I always use j601
beta), but hopefully I've not introduced any horrible typos
here that would keep these from working on j504:

inc0=: verb define
   y. + x. e.i.# y.
)

inc1=: verb def 'y.+x. e.~i.# y.'

inc2=: 13 :'y.+x. e.~i.# y.'

inc3=: ] + [EMAIL PROTECTED]@] e. [

All of these are just ways of expressing the same algorithm.

The decrement version is a straightforward transformation
of the above -- just replace + with -

You might note that none of these use the increment operator.

In part, that's because my problem statement did not include
"use the increment operator".  In part, that's because I didn't
have any use for the increment operator.  Note that I could add
the increment operator to the above statements, but all that would 
do is add extra complexity -- even if I use the result from the
increment operator.

However, for completeness, here's a crude way of implementing 
integer + using >: -- once you've done this, you can plug in this
alternative add implementation into any of the above expressions.

   add=: >:@]^:[ ]

However, be careful with this operation -- it actually implements
an outer product form of addition:
   1 2 3 add 1 1 
2 2 1
3 3 1
4 4 1

If you want it to be a true replacement for + you'd need to use
it at rank 0
   add0=:(>:@]^:[ ])"0
   1 2 3 add0 1 1 1
2 3 4

Subtraction can be achieved by replacing >: with <: or by negating
the second argument.

Of course, using + is going to be simpler and more efficient.

I could also discuss alternative ways of manipulating sequences,
using indexing.  But, I think other people have already talked 
about that quite a bit.  So I'm going to fall back on some general
statements:

When you're trying to solve problems for real, it's usually best 
to start with a simple statement of the basic requirements and 
work from there.

Artificial constraints can be useful for educational purposes,
or when working within a medium (for example: poetry) but even 
there it's often good to take a look at how to approach
the problem with the constraints lifted.

-- 
Raul

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to