[racket] Why no string function to replace substring based on content in Racket

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Tue Feb 7 22:40:37 EST 2012

1. I am surprised that regexp-replace* does not work on input-ports like all other regexp- functions. I assume Matthew has a rationale but my quick look didn't discover it in the docs. 

2. I ran this stupid little program below. The timing for a file of 19990186 chars over 9896 lines, with 9922 occurrences of "abc" clocks in like this: 

cpu time: 4570 real time: 4629 gc time: 2164

How much too slow is this? -- Matthias


#lang typed/racket

(define file "/tmp/test.txt")
#;
(with-output-to-file file
  #:exists 'replace
  (lambda ()
    (for: ((i (in-range 10000000)))
          (define r (random 1000))
          (cond
            [(= r 13) (display "abc")]
            [(= r 27) (newline)]
            [else (display "de")]))))

(: f (-> (Listof String)))
(define (f)
  (define next (read-line))
  (if (eof-object? next)
      '()
      (cons (regexp-replace* next "abc" "xy") (f))))

(collect-garbage)
(collect-garbage)
(collect-garbage)
(define x (time (with-input-from-file file f)))








On Feb 7, 2012, at 8:05 PM, Harry Spier wrote:

> I wasn't able to find in the Racket documentation a string function
> that replaces substrings in a string based on the content of the
> substring.  Something similar to Python's string replace method .
> s = "abcdefabcdef"
> s.replace("abc" "123")  -> "123def123def"
> 
> But more surprising, I also wasn't able to find such a function in
> SRFI13 which is supposed to be a comprehensive set of operations on
> strings.
> Its string-replace function is based on position not on content of the
> substring.
> 
> Is there some functional programming reason for not having such a
> built-in function in Racket/Scheme, or is the idea that you use
> regular expressions to do this.
> My understanding is that regular expressions can be quite expensive.
> I have Python programs which convert Indian language book length
> etexts from one transliteration scheme to another so I'm calling the
> Python  replace function hundreds of thousands of times per e-text and
> doing multiple replaces with each call.
> 
> I'd like to convert these programs to Racket, but using regular
> expressions might be too slow.  Is  writing a C foreign function.
> extension my only alternative or is there such a function in some
> Racket package..
> 
> Thanks,
> Harry Spier
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



Posted on the users mailing list.