更改字符串中的某些字母
编辑:
(define leet-helper
(lambda (string)
(cond
[(null? (string->list string)) ""]
[(equal? (car string)#\e)
(cons 3 (leet-speak (cdr string)))]
[(equal? (car string) #\s)
(cons 5 (leet-speak (cdr string)))]
[(equal? (car string) #\o)
(cons 0 (leet-speak (cdr string)))]
[else string])))
(define leet-speak
(lambda (string)
(list->string (string->list (leet-speak string)))))
我正在解决一个问题,它接受一个字符串并返回相同的字符串,其中所有 e 都变成 3,所有 s 都变成 5,所有 o 都变成 0。我发布了到目前为止我创建的内容,但是当我尝试测试它时,我不断收到错误,所以我知道我没有做正确的事情。另外..此代码有 3 个独立的条件,但如果给定的字符串是“eso”,我希望更改所有 3 个字符,而不仅仅是“e”。我不确定如何做到这一点,或者我的递归是否会自行解决这个问题[?]。任何指示将不胜感激!
(define leet-speak
(lambda (string)
(cond
[(null? (string->list string)) ""]
[(equal? (car (string->list string)) 'e)
(list->string (cons 3 (leet-speak (cdr (string->list string)))))]
[(equal? (string->list (car string)) 's)
(list->string (cons 5 (leet-speak (cdr (string->list string)))))]
[(equal? (string->list (car string)) 'o)
(list->string (cons 0 (leet-speak (cdr (string->list string)))))])))
Edit:
(define leet-helper
(lambda (string)
(cond
[(null? (string->list string)) ""]
[(equal? (car string)#\e)
(cons 3 (leet-speak (cdr string)))]
[(equal? (car string) #\s)
(cons 5 (leet-speak (cdr string)))]
[(equal? (car string) #\o)
(cons 0 (leet-speak (cdr string)))]
[else string])))
(define leet-speak
(lambda (string)
(list->string (string->list (leet-speak string)))))
I'm working on a problem that takes a string and returns the same string with all e's turned into 3's, all s's turned into 5's, and all o's turned into 0's. I posted what I have created so far, but I keep getting errors when I try to test this so I know I'm not doing something right. Also.. this code has 3 separate conditionals, but if the string given is "eso", I want all 3 characters to be changed, not just the 'e'. I'm not sure how to do that, or if my recursion will take care of that by itself [?]. Any pointers would be appreciated!
(define leet-speak
(lambda (string)
(cond
[(null? (string->list string)) ""]
[(equal? (car (string->list string)) 'e)
(list->string (cons 3 (leet-speak (cdr (string->list string)))))]
[(equal? (string->list (car string)) 's)
(list->string (cons 5 (leet-speak (cdr (string->list string)))))]
[(equal? (string->list (car string)) 'o)
(list->string (cons 0 (leet-speak (cdr (string->list string)))))])))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在假设我们使用字符串“test”测试您的程序(通过手动单步执行)。我们看到的第一个错误是您没有
else
案例。其次,您会注意到,当您将(car (string->list string))
与'e
进行比较时,您总是会得到 false,因为当字符串被分解时,它是分解为字符 (#\e
) 而不是符号。我建议,当您测试代码时,记下不断出现的错误(什么样的错误?您在问题中没有提及它们;方案通常会提供非常丰富的错误消息)并尝试修复每个错误其中。例如,如果您得到
,那么您就知道您提供的
string->list
的myinput
类型有问题。此类错误在您的代码中多次出现。当我遇到递归输入类型问题时,我喜欢使用的一种方法是使用辅助函数/包装函数(提示)。此外,测试递归程序的一个好方法是手动运行它们(例如,使用字符串“test”,如开头所述),然后查看卡在哪里并相应地修复程序。
我希望这将帮助您找到正确的方向,如果您需要更多指导,请告诉我。
Now suppose that we test your program (by stepping manually) with a string "test". The first error we see is that you have no
else
case. Second, you will notice that when you compare(car (string->list string))
with'e
you will always get false because when a string is decomposed it is decomposed as characters (#\e
) and not symbols.I would suggest that when you test your code, take note of the errors that you keep getting (what kind of errors? you don't mention them in your question; scheme is usually pretty informative with its error messages) and try to fix each of them. For instance, if you get
then you know that there is something wrong with the type of
myinput
that you are givingstring->list
. This type of errors occurs a few time in your code. One way that I like to use when I have problems with recursion input types is to use helper functions/wrapper function (hint).Moreover, a good way to test recursive programs is to run through them manually (e.g. with a string "test", as mentioned in the beginning) and then see where you get stuck and fix the program accordingly.
I hope that this will help set you in the right direction, and let me know if you need more pointers.