0

所以当我提出这个问题时,我正在练习球拍初学者语言。

编写一个str-replace使用字符串、目标字符和替换字符的函数。该函数生成一个新字符串,该字符串与使用的字符串相同,其中所有出现的目标字符(如果有)都替换为替换字符。例如,(string-replace "word" #\o #\y) ⇒ "wyrd"

string->list注意:我不能使用除and之外的任何内置字符串函数list->string

所以我从代码开始,现在我卡住了,我如何使用这个代码的包装函数,到目前为止我只有这个

;; los is list of string      
(define(str-replace los)
   (+(first los)
   (first (rest los))
   (first (rest (rest los)))
   (first (rest (rest (rest los))))))
4

1 回答 1

2

Define a conversion function which operates on lists:

(define (replace-in-list input-list from-char to-char)
  (if (null? input-list)
      ...
      (cons ... 
            (replace-in-list ... from-char to-char))))

(You have to fill the blank ...)

And call it from another one:

(define (str-replace input-string from-char to-char)
  (list->string 
    (replace-in-list 
      (string->list input-string) from-char to-char)))
于 2016-10-15T20:53:53.243 回答