【发布时间】:2014-11-23 06:44:32
【问题描述】:
如何将find-string抽象为generic-find-string,使其使用的字符串比较操作是一个参数。使用本地?或任何其他抽象函数?
我的 find-string 通过递归,
;; find-string: Lof[string] string -> Boolean
;; true if and only if the given string was in the list
(define (find-string los s)
(cond
[(empty? los) false]
[(cons? los) (cond
[(string=? s (first los)) true]
[else (find-string (rest los) s)])]))
(check-expect(find-string (list "a" "b" "c") "a") true)
(check-expect(find-string (list "a" "b") "f") false)
(check-expect(find-string empty "a") false)
我想我们可以使用 local (也许不是?它看起来也像 map 或 filter),而 generic-find-string 的合同是,
;; generic-find-string: (Lof[string] string -> Boolean ) Lof[string] -> Boolean
;; true if and only if the given string was in the list
然后用这个抽象来定义find-string-case-sensitive,应该和原来的find-string一样操作,find-string-case-insensitive,和find-string-case-insensitive一样契约为 find-string 但在比较字符串时忽略字母字符的大小写(即字符 a 被认为与 A 相同,依此类推;非字母字符仍然必须完全匹配)。
有什么想法和建议吗?提前致谢。
【问题讨论】: