Ethiopian multiplication: Difference between revisions
Content added Content deleted
Line 1,555: | Line 1,555: | ||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
⚫ | |||
The first implementation below relies on the representation of natural numbers as lists of bits, |
|||
⚫ | |||
lsb first, so that halving is the same as taking the tail, doubling |
|||
⚫ | |||
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> |
||
⚫ | |||
#import nat |
|||
⚫ | |||
⚫ | |||
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 |
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> |
||
⚫ | |||
⚫ | |||
⚫ | |||
be redefined in practice. To comply with the task specification, they can be defined |
|||
as follows. |
|||
<lang Ursala> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
</lang> |
|||
test program: |
test program: |
||
<lang Ursala> |
<lang Ursala> |