[英]In racket how do I replace word in string using string->list or list->string function only?
So I was practicing racket beginner language when I came along this question. 所以当我遇到这个问题时,我正在练习球拍初学者的语言。
Write a function str-replace
which consumes a string, a target character, and a replacement character. 编写一个使用字符串,目标字符和替换字符的函数str-replace
。 The function produces a new string, which is identical to the consumed string with all occurrences of the target character (if any) replaced with the replacement character. 该函数产生一个新字符串,该字符串与消耗的字符串相同,所有出现的目标字符(如果有)都被替换字符替换。 For example, (string-replace "word" #\\o #\\y) ⇒ "wyrd"
. 例如, (string-replace "word" #\\o #\\y) ⇒ "wyrd"
。
Note: I may not use any built-in string functions other than string->list
and list->string
. 注意:除了string->list
和list->string
我不能使用任何内置的字符串函数。
So I started with the code now I got stuck, how do I use wrapper function for this code as far now I have only this 所以我从卡住的代码开始,到现在为止我只有这个
;; los is list of string
(define(str-replace los)
(+(first los)
(first (rest los))
(first (rest (rest los)))
(first (rest (rest (rest los))))))
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)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.