Re: no char->string / string-append



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
.



Relevant Pages

  • Re: no char->string / string-append
    ... > Do I have to use make-string to append chars onto the end of ... here really indicates a non-constant character. ... Appending strings is slow. ...
    (comp.lang.scheme)
  • no char->string / string-append
    ... Do I have to use make-string to append chars onto the end of ... I suppose the real solution would be to not use string-append at ...
    (comp.lang.scheme)
  • Re: woo hooo
    ... I think the only hitch I'm having with it is plugging into my Voodoo lab Pedal Power 2 Plus. ... They reckon it's not designed for "grunt" but more a bluesy kinda sound. ... Really really nice mids, lovely full bodied bass notes, and the type of glassy top end you'd get with a nice compressor but without the bullshit noise. ... I adjusted the pickups last night by taking them away from the strings a millimetre or two and altered the action so it wasn't so low but at the same time not so high either. ...
    (alt.guitar)
  • Re: fukkin a
    ... Power chords are two or three strings, ... barre chords. ...
    (alt.guitar)
  • Re: fukkin a
    ... Power chords are two or three strings, ... barre chords. ...
    (alt.guitar)