Ethiopian multiplication: Difference between revisions

Content added Content deleted
Line 1,555: Line 1,555:


=={{header|Ursala}}==
=={{header|Ursala}}==
This solution makes use of the functions odd, double, and half, which respectively
The first implementation below relies on the representation of natural numbers as lists of bits,
check the parity, double a given natural number, or perform truncating division by two. These
lsb first, so that halving is the same as taking the tail, doubling
functions are normally imported from the nat library but defined here explicitly for
is inserting a zero, and filtering for oddness is filtering by the head,
the sake of completeness.
all of which are expressible as primitives.
<lang Ursala>
<lang Ursala>
odd = ~&ihB
#import nat
double = ~&iNiCB

half = ~&itB
emul = @lrtK33PDNlCrXK9rhPFlS sum:-0
</lang>
</lang>
The functions above are defined in terms of bit manipulations exploiting the concrete representations
Here is a more verbose version (in keeping with the spirit of the task) treating natural numbers
as abstract types, and explicitly using the operators for distribution (*-), triangular iteration (|\),
of natural numbers. The remaining code treats natural numbers instead as abstract types by way of the library API, and uses the operators for distribution (*-), triangular iteration (|\),
and filtering (*~) among others.
and filtering (*~) among others.
<lang Ursala>
<lang Ursala>
Line 1,572: Line 1,572:
emul = sum:-0@rS+ odd@l*~+ ^|(~&,double)|\+ *-^|\~& @iNC ~&h~=0->tx :^/half@h ~&
emul = sum:-0@rS+ odd@l*~+ ^|(~&,double)|\+ *-^|\~& @iNC ~&h~=0->tx :^/half@h ~&
</lang>
</lang>

The program above makes use of the functions odd, double, and half, which respectively
check for whether or not a number is even, double a given number, or perform truncating
division by two. These functions are imported from the nat library and would not be
be redefined in practice. To comply with the task specification, they can be defined
as follows.
<lang Ursala>
odd = ~&ihB
double = ~&iNiCB
half = ~&itB
</lang>
test program:
test program:
<lang Ursala>
<lang Ursala>