I'm just starting out with LISP so we are asked to just write the
program with a list of 1's and 0's... no bit vectors or anything
So in converting binary to decimal, I'm trying to figure out how to
recursively do this...
I know it would be some form of
last binnum * 1 + last (butlast binnum) * 2 + last (butlast (butlast
binnum)) * 2 * 2
and so on...
but in terms of putting that in a recursive LISP function... how would
I do this? Any help? :-)
The binary number is supposed to be of variable length
········@aol.com (Miss Coffee Bean) writes:
> I'm just starting out with LISP so we are asked to just write the
> program with a list of 1's and 0's... no bit vectors or anything
>
> So in converting binary to decimal, I'm trying to figure out how to
> recursively do this...
>
> I know it would be some form of
>
> last binnum * 1 + last (butlast binnum) * 2 + last (butlast (butlast
> binnum)) * 2 * 2
>
> and so on...
>
> but in terms of putting that in a recursive LISP function... how would
> I do this? Any help? :-)
> The binary number is supposed to be of variable length
Think about how a binary is build it's something like
2^n + 2^(n-1) + .... 2^0
for (101) it is 2^2*1 + 2^1*0 + 2^0*1 = 4 + 0 + 1 = 5
I suggest starting from the back. And than it's no too difficult.
Lisp has nice things like reverse to get you going.
Regards
Friedrich
Friedrich Dominicus <·····@q-software-solutions.com> writes:
> Think about how a binary is build it's something like
>
> 2^n + 2^(n-1) + .... 2^0
>
> for (101) it is 2^2*1 + 2^1*0 + 2^0*1 = 4 + 0 + 1 = 5
>
> I suggest starting from the back.
i'd suggest starting from the front and using horner's scheme.
double, add, double, add, double, add.
--
J o h a n K u l l s t a m
[········@ne.mediaone.net]
sysengr
Johan Kullstam <········@ne.mediaone.net> writes:
>
> i'd suggest starting from the front and using horner's scheme.
> double, add, double, add, double, add.
Yes I think you are right it's better.
Regards
Friedrich
Thore B. Karlsen <········@cs.utexas.edu> writes:
>
> I posted code that did just that. Surprisingly, nobody seems to have
> taken notice of it but keep discussing an inferior solution.
Sometimes one can not see forrest just the tree. I guess the Horner
Scheme is the standard way and "optimal" for transferring from one
base to another. I simply forget it. I guess the other solution is
still discussed because the OP posted his attempts and other comment
on this.
Regards
Friedrich