3

Here's a function to calculate the digital sum of a number in J:

digitalSum =: +/@:("."0)@":

If I use b. to query the rank of this verb, I get _ 1 _, i.e., infinite. (We can ignore the dyadic case since digitalSum is not dyadic.)

I would like the monadic rank of this verb to be 0, as reported by b.. The only way I know of to do this is to use a "shim", e.g.,

ds =: +/@:("."0)@":
digitalSum =: ds"0

This works great, but I want to know whether it's the only way to do this, or if there's something else I'm missing.

Clarification

I just discovered how to change the rank of a verb that's defined thus:

digits =: 3 : 0 "0
    "."0@": y
)

Notice the "0 after the declaring 3 : 0. You can put any adverb or conjunction you wish, and it will be applied to the verb as a whole. Pretty cool stuff!

Gregory Higley
  • 15,923
  • 9
  • 67
  • 96
  • Yet again, I may have answered my own question: `digitalSum =: (+/@:("."0)@":)"0` However, perhaps there's another way, so I'll leave the question open. – Gregory Higley Jun 02 '10 at 22:23

1 Answers1

2

digitalSum =: (+/@:("."0)@":)"0 is how I would define it as well. Using " to change rank is pretty standard and works on parenthesized tacit trains.

David
  • 7,011
  • 1
  • 42
  • 38