strange number base converter
Author Message
strange number base converter

it converts numbers from/to base 2..10.
do you see, how it works?

to fromTo10 :fromBase :toBase :n
op reduce [[a b] [op :b * :fromBase + :a]] ~
(map [[a] [op remainder :a :toBase]] ~
(cascade [?2=0] [lput ?2 ?1] [] ~
[int(?2/:toBase)] :n))
end

to fromToAny :fromBase :toBase :n
op fromTo10 10 :toBase (fromTo10 :fromBase 10 :n)
end

enjoy :-),
--
Samuel Kilchenmann

p.s.:
this is the original Haskell code, which i tried to translate to LOGO:

fromTo n m = foldr (\a b -> b * n + a) 0 . digits m
where digits j n = map (`mod` j) ((takeWhile (>0) . iterate (`div` j)) n)

has LOGO something similar to the "functional composition" operator '.'?
is there a LOGO equivalent to "partial application"?

Fri, 03 Mar 2000 03:00:00 GMT
strange number base converter

Quote:

>to fromTo10 :fromBase :toBase :n
>   op reduce [[a b] [op :b * :fromBase + :a]] ~
>             (map [[a] [op remainder :a :toBase]] ~
>                  (cascade [?2=0] [lput ?2 ?1] [] ~
>                                  [int(?2/:toBase)] :n))
>end

Wow, what a hack!  This code is a pun -- what it does converting
into decimal is entirely unrelated to what it does converting
out of decimal.  Very enjoyable, but weird!

Converting from decimal:  The MAP works out the digits of the
converted number in the other base the same way you'd do it on
paper, and the REDUCE merely puts those digits together into
a word.

Converting to decimal:  The MAP merely turns the input numeral
into a list of digits, and the REDUCE computes the numeric value
of those digits the same way you'd do it on paper.

Of course a more straightforward Logo solution would use the
word operations (word, first, butfirst, etc) to do the parts
I've described as "merely" above.

Fri, 03 Mar 2000 03:00:00 GMT
strange number base converter

agreeing with briian i think that samuel's hack
is very nice.

however, as ins the case of the "sum of digits factorials = value"
and even more so,
i think that for problems with nuber representaytions
we should make it very clear that
this also works outside the decimal system.

a conversion routine should "open the door"
to base > 10

here is
what i concocted
using samuel's ideas
-=-=-=-=-=-=-=-=-=-=

to convert :s :frombase :tobase
output number.string (num.val :s :frombase) :tobase
end

to digit.symb :val :base
output item :val + 1 digitlist :base
end

to digit.symb.seq :valseq :base
output reduce [word (digit.symb ?1 :base) ?2] ~
reverse fput (word) :valseq
end

to digit.val :sym :base
output (listpos :sym digitlist :base) - 1
end

to digit.val.seq :n :base
output (map [[a] [op remainder :a :base]] ~
(cascade [?2=0] [lput ?2 ?1] [] ~
[int(?2/:base)] :n))
end

to digitlist :base
output [0 1 2 3 4 5 6 7 8 9 ~
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z]
end

to explode :w
if emptyp :w [output []]
output fput first :w explode butfirst :w
end

to listpos :obj :l
if emptyp :l [output 0]
if :obj = first :l [output 1]
output 1 + listpos :obj butfirst :l
end

to num.val :digitword :base
output  reduce [[a b] ((:b * :base) + :a)] ~
map [digit.val ? :base] reverse explode :digitword
end

to number.string :n :base
output digit.symb.seq (digit.val.seq :n :base) :base
end

: it converts numbers from/to base 2..10.
: do you see, how it works?

: to fromTo10 :fromBase :toBase :n
:    op reduce [[a b] [op :b * :fromBase + :a]] ~
:              (map [[a] [op remainder :a :toBase]] ~
:                   (cascade [?2=0] [lput ?2 ?1] [] ~
:                                   [int(?2/:toBase)] :n))
: end

: to fromToAny :fromBase :toBase :n
:    op fromTo10 10 :toBase (fromTo10 :fromBase 10 :n)
: end

: enjoy :-),
: --
: Samuel Kilchenmann

: p.s.:
: this is the original Haskell code, which i tried to translate to LOGO:

: fromTo n m = foldr (\a b -> b * n + a) 0 . digits m
:  where digits j n = map (`mod` j) ((takeWhile (>0) . iterate (`div` j)) n)

: has LOGO something similar to the "functional composition" operator '.'?
: is there a LOGO equivalent to "partial application"?

Sat, 04 Mar 2000 03:00:00 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages