【发布时间】:2020-02-29 03:31:51
【问题描述】:
在 Racket 中测试字符串中子字符串的出现。
我的代码如下所示。
(define (check-for-substring/list loc loc-to-find)
(cond [(empty? loc-to-find) true]
[(empty? loc) false]
[(char=? (first loc) (first loc-to-find))
(or (check-for-substring/list (rest loc) (rest loc-to-find))
(check-for-substring/list (rest loc) loc-to-find))]
[else (check-for-substring/list (rest loc) loc-to-find)]))
(define (check-for-substring string substring)
(check-for-substring/list (string->list string) (string->list substring)))
测试示例如下所示。
(check-expect (check-for-substring "flag" "flagged") false)
(check-expect (check-for-substring "flagged" "flag") true)
(check-expect (check-for-substring "" "") true)
(check-expect (check-for-substring "a" "") true)
不适用于我的代码的测试示例:
(check-expect (check-for-substring "flaminegio" "flamingo") false)
(check-expect (check-for-substring "heiloght" "height") false)
注意:对于那些不熟悉具有列表缩写的初学者语言的人,没有允许在函数中定义函数的“本地”函数,并且使用“列表”而不是“缺点”。此外,“length”函数输出列表的长度,而不是字符串的长度。您可以在 cmets 中要求更多说明。
【问题讨论】:
-
@tjorchrt 抱歉。只是你的代码不起作用。有一个奇怪的函数“take”。
-
我已经提到了#lang racket。