Re: no char->string / string-append



On 2006-01-27, Lauri Alanko <la@xxxxxx> wrote:
> In article <slrndtl450.1pc.leadvoice@xxxxxxxxxxxxxxxxxx>,
> Neil Cerutti <leadvoice@xxxxxxxxx> wrote:
>> I decided to build up a list after all, though I have to
>> reverse it at the end. I believe that's a scheme idiom, isn't
>> it?
>
> Yes, but in this case it's not necessary.

Ah, thanks for this prodding. I'd never have figured out
how to work from least significant digit up, otherwise.

>> Criticism is welcome. Particularly suggestions for splitting
>> it up into more digestible functions.
>
> Your function is too complex, in the sense that it does too
> much work to get to the result. And it is too simple, in that
> it doesn't treat all cases correctly: what if number is zero?

If the number is zero it seems to have worked OK, returning (0)
as expected.

(convert 0 12) --> (0)
(convert 0 2) --> (0)

> Hints: you don't need the power function. You don't need to
> reverse anything. You do need the modulo function.

Thanks for the tips.

Here's V0.2:

; Convert [ number:Integer base:Integer ] -> List
; Convert number into a List representation of a number in base. Traditional
; hexadecimal digits are not used. Bases less than 2 cause the function to
; return #f. The intent is that the result can be easily translated into a
; notation of your choice.
; (convert 10 2) --> (1 0 1 0)
; (convert -20 4) --> (- 1 1 0)
; (convert 100 17) --> (5 15)
(define (convert number base)
(if (< base 2)
#f
(let loop ((n (abs number))
(result '()))
(if (< n base)
(if (negative? number)
(cons '- (cons n result))
(cons n result))
(loop (quotient n base) (cons (modulo n base) result))))))

It was with some consternation that I discovered my first attempt
at this new procedure would have listed the digits in reverse.

--
Neil Cerutti
.



Relevant Pages

  • Re: More number puzzles
    ... There are too many ways to "invert" (flip the page upside down, ... reciprocal of the number, reverse all of the digits, reverse pairs of ... digits, reverse triplets of digits, take complements of each digit, flip ... the puzzle in the message, enter the number into Google and discover the ...
    (rec.puzzles)
  • Re: Numbers that have the same digits as their factors.
    ... That's a good question about why the factors in this example are in reverse ... However zero behaves ... differently than all the other digits if you are manipulating digits. ...
    (sci.math)
  • Re: More number puzzles
    ... There are too many ways to "invert" (flip the page upside down, ... reciprocal of the number, reverse all of the digits, reverse pairs of ... digits, reverse triplets of digits, take complements of each digit, flip ... the puzzle in the message, enter the number into Google and discover the ...
    (rec.puzzles)
  • Re: Enigma 1497 - Divide both ways
    ... New Scientist magazine, 7 June 2008. ... By Richard England. ... Using each of the digits 1 to 9, ... whose reverse is an integer also divisible ...
    (rec.puzzles)
  • Re: More number puzzles
    ... Is your puzzle just to "invert" the number? ... reciprocal of the number, reverse all of the digits, reverse pairs of ... digits, reverse triplets of digits, take complements of each digit, flip ...
    (rec.puzzles)