Keep it simple, please.

---------------------------

From "Learning J":

*To compute the sum of a list of numbers, we have seen the verb +/ but let
us look at another way of defining a summing verb.*

*The sum of an empty list of numbers is zero, and otherwise the sum is the
first item plus the sum of the remaining items. If we define three verbs,
to test for an empty list, to take the first item and to take the remaining
items:*

*   empty =: # = 0:*
*   first =: {.*
*   rest  =: }.*

*then the two cases to consider are:*

*an empty list, in which case we apply the 0: function to return zero*

*a non-empty list, in which case we want the first plus the sum of the
rest:*

*   Sum =: (first + Sum @ rest) ` 0:  @. empty *

*   Sum 1 1 2*
*4*
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to