Racket 在文本文件中查找empy行

Racket 在文本文件中查找empy行,racket,Racket,我已经学习racket几天了,我对这个任务感到困惑,我试图在文本文件中找到空行,并随机选择一个空行插入文本“calculation here”,这是我迄今为止所做的 例如:myfile.txt包含以下内容: line1 line2 line3 line4 运行脚本后,myfile.txt现在应该如下所示: line1 calculation here line2 line3 line4 或: 联合国工作代码如下: #lang racket (define (write-to-file

我已经学习racket几天了,我对这个任务感到困惑,我试图在文本文件中找到空行,并随机选择一个空行插入文本“calculation here”,这是我迄今为止所做的

例如:myfile.txt包含以下内容:

line1

line2
line3

line4
运行脚本后,myfile.txt现在应该如下所示:

line1
calculation here
line2
line3

line4
或:

联合国工作代码如下:

#lang racket

(define (write-to-file host text) (
             with-output-to-file host (                 
             lambda () (
             write text))
             #:exists 'replace))

(define empty-lines '()) ;store line number of empty line (if any)

(define (file-lines text-file) 
                (file->lines text-file))

(define (add-to-list line-num)
  (set! empty-lines (cons line-num empty-lines)))

(let loop ((l (file-lines "myfile.txt")))
   (cond ((null? l) #f)
         (else
           (printf "~s\n" (first l)) ; just for debugging
           (cond ((equal? (first l) "") (add-to-list (first l)))(else #f))
           (loop (rest l)))))

;now i need to select a random line from the list of empty-lines.
;and write "calculations here" to that line

我使用的读取行方法没有问题,问题是检测并选择一个随机的空白来插入我的文本。

在文件中查找2个并发\n。我很肯定在球拍里有办法做到这一点。将这些索引存储在列表中随机选择一对,并将第二个索引替换为“calculation here\n”

此函数可以帮助您逐行读取文件。
检查长度是否为0。并将该行替换为注释

给定文件名,可以使用将其读入行列表。例如:

(for ([line (in-list (file->lines "some-file"))])
  (displayln (cond [(zero? (string-length line)) (make-random-line)]
                   [else line])))
其中,
make random line
是您定义用于返回 随机字符串,就像你说的那样

上面的代码将整个文件读取到内存中的列表中。对于较大的文件,最好逐行处理。可以使用以下顺序执行此操作:

(with-input-from-file "some-file"
  (thunk
   (for ([line (in-lines)])
     (displayln (cond [(zero? (string-length line)) (make-random-line)]
                      [else line])))))

更新 现在我明白了你的问题:

#lang racket

(define lines (file->lines "some-file-name"))

(define empty-line-numbers (for/list ([line (in-list lines)]
                                      [n    (in-naturals)]
                                      #:when (zero? (string-length line)))
                             n))

(define random-line-number (list-ref empty-line-numbers
                                     (random (length empty-line-numbers))))

(for ([line (in-list lines)]
      [n    (in-naturals)])
  (displayln (cond [(= n random-line-number) "SOME NEW STRING"]
                   [else line])))

不太熟悉racket,但如果我这么做,这将是我的逻辑(定义(读取下一行iter文件)(让((行(读取行文件)))(除非(eof对象?行)(显示行)(换行)(读取下一行iter文件)))(使用输入文件“foobar.txt”调用读取下一行iter)读取文件工作正常,我得到列表中的所有行,它会迭代它们。问题是将空行的索引添加到空列表中,并在文件中选择一个随机位置插入文本谢谢您的帮助,但我从未说过我想返回任何类型的随机字符串,我说过我需要在所述文件中的随机空白处放置一个字符串。我将重新表述我的问题。你做自由职业吗?如果可以的话,你能和我联系吗。。谢谢你的回复。似乎在freegorer.com上没有针对球拍程序员的部分:/
(with-input-from-file "some-file"
  (thunk
   (for ([line (in-lines)])
     (displayln (cond [(zero? (string-length line)) (make-random-line)]
                      [else line])))))
#lang racket

(define lines (file->lines "some-file-name"))

(define empty-line-numbers (for/list ([line (in-list lines)]
                                      [n    (in-naturals)]
                                      #:when (zero? (string-length line)))
                             n))

(define random-line-number (list-ref empty-line-numbers
                                     (random (length empty-line-numbers))))

(for ([line (in-list lines)]
      [n    (in-naturals)])
  (displayln (cond [(= n random-line-number) "SOME NEW STRING"]
                   [else line])))