factorial=: !

factorial=: (* $:@<:)`1:@.(1>])"0

Or, if you prefer:

factorial=: (* factorial@<:)`1:@.(1>])"0

Note that (* $:@<:)`1:@.(1>])"0 is far less efficient than ! but at
least it's less general.

I hope this helps,

-- 
Raul


On Mon, Jun 5, 2017 at 10:40 AM, Michael Rice <[email protected]> wrote:
> 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to