Ethiopian multiplication: Difference between revisions

m
Line 1,555:
 
=={{header|Ursala}}==
TheThis program abovesolution 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 forthe whether or not a number is evenparity, 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
division by two. These functions are normally imported from the nat library andbut defined wouldhere notexplicitly befor
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>
odd = ~&ihB
#import nat
double = ~&iNiCB
 
half = ~&itB
emul = @lrtK33PDNlCrXK9rhPFlS sum:-0
</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
of natural numbers. The remaining code treats natural numbers instead as abstract types by way of the library API, and explicitly usinguses the operators for distribution (*-), triangular iteration (|\),
and filtering (*~) among others.
<lang Ursala>
Line 1,572:
emul = sum:-0@rS+ odd@l*~+ ^|(~&,double)|\+ *-^|\~& @iNC ~&h~=0->tx :^/half@h ~&
</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:
<lang Ursala>
Anonymous user