Am Montag, 24. März 2014, 01:19:12 schrieb Mark H Weaver: > Andy Wingo <[email protected]> writes: > > > On Fri 13 Sep 2013 21:41, Mark H Weaver <[email protected]> writes: > > > > Inspired to code-golf a bit, here's one that's even faster :) > > > > (define (string-replace-substring s substring replacement) > > "Replace every instance of substring in s by replacement." > > (let ((sublen (string-length substring))) > > (with-output-to-string > > (lambda () > > (let lp ((start 0)) > > (cond > > ((string-contains s substring start) > > => (lambda (end) > > (display (substring/shared s start end)) > > (display replacement) > > (lp (+ end sublen)))) > > (else > > (display (substring/shared s start))))))))) > > > > Just marginally so, though.
Nice! > One warning though: in Guile 2.0, string ports only support characters > representable in the %default-port-encoding Then I’ll keep the original one for the time being. How are the chances of getting this function into regular Guile? If nothing else, then the speedup of more than factor 80 over my naive implementation would merit providing the efficient one to all guile users, I think. (Also I think that replacing a substring is a very common operation when working with text - like serving websites) Best wishes, Arne -- Unpolitisch sein heißt politisch sein, ohne es zu merken. - Arne (http://draketo.de)
