Re: no char->string / string-append
- From: Neil Cerutti <leadvoice@xxxxxxxxx>
- Date: 27 Jan 2006 22:29:30 +0100
On 2006-01-27, Lauri Alanko <la@xxxxxx> wrote:
> In article <slrndtkkud.1pc.leadvoice@xxxxxxxxxxxxxxxxxx>,
> Neil Cerutti <leadvoice@xxxxxxxxx> wrote:
>> Do I have to use make-string to append chars onto the end of
>> strings? A la
>>
>> (string-append "Hello" (make-string 1 #\!))
>
> Well, (string-append "Hello" (string #\!)) is marginally simpler. I'm
> assuming that #\! here really indicates a non-constant character.
>
>> I suppose the real solution would be to not use string-append at
>> all. Any advice?
>
> Appending strings is slow. Use string ports (srfi-6) if you
> have them.
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? Here's
the program so far.
Criticism is welcome. Particularly suggestions for splitting it
up into more digestible functions.
Moreover, the algorithm was written basically as it occurred to
me, and then slightly refactored. I could not think how to break
it down before writing any code.
; Convert [ Integer:number Integer:base ] -> 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)
(define (power n)
(if (>= (expt base n) (abs number))
(1- n)
(power (1+ n))))
(if (< base 2)
#f
(let convert-do ((n (abs number))
(power (power 1))
(result (if (< number 0) '(-) '())))
(if (zero? power)
(reverse (cons n result))
(let* ((multiplier (expt base power))
(digit (quotient n multiplier)))
(convert-do (- n (* digit multiplier))
(1- power)
(cons digit result)))))))
--
Neil Cerutti
.
- Follow-Ups:
- Re: no char->string / string-append
- From: Lauri Alanko
- Re: no char->string / string-append
- References:
- no char->string / string-append
- From: Neil Cerutti
- Re: no char->string / string-append
- From: Lauri Alanko
- no char->string / string-append
- Prev by Date: Re: arrays vs. vectors
- Next by Date: Re: no char->string / string-append
- Previous by thread: Re: no char->string / string-append
- Next by thread: Re: no char->string / string-append
- Index(es):
Relevant Pages
|