返回索引或子字符串,而不是符号
在您的问题中,您使用了以下示例:
(string-include 'abd 'abbbe) => nil
(string-include 'ghf 'dghfd) => ghf
假设您要返回 symbols nil 和 ghf,如果您想检查是否一个字符串包含子字符串 NIL。例如,通过这种方法,您将拥有:
(string-include 'nil 'vanilla) => nil
返回 nil 是不是因为 "NIL" 在 "VANILLA" 中,因为它不是?这是模棱两可的,你不能说。相反,您可以返回实际的字符串,因为 string "NIL" 是一个真值。更好的是,如果您返回字符串的 index,那么您会发现 where 在另一个字符串中第一个字符串出现。例如,这就是内置函数 search 的行为方式。
直接,使用 SEARCH
你可以用search来实现这个:
(defun substringp (needle haystack &key (test 'char=))
"Returns the index of the first occurrence of the string designated
by NEEDLE within the string designated by HAYSTACK, or NIL if it does
not occur. Characters within the string are compared by TEST, which
defaults to CHAR= (for case-sensitive comparison)."
(search (string needle)
(string haystack)
:test test))
注意使用string 函数将string designators(字符、字符串和符号)转换为它们指定的字符串。请记住,在标准设置中,阅读器会将符号名称大写,因此符号 cat 表示字符串 "CAT"。最后,由于这会返回 search 的结果,它对您有双重作用:如果有一个出现,它会返回第一次出现的 index,而 nil 否则。请记住,除了 nil 之外的所有内容都是 true 值(甚至为 0),因此您可以将结果用作布尔值或索引(只要您检查它不是无)。以下是一些示例:
CL-USER> (substringp "cat" "concatenate")
3
CL-USER> (substringp "dog" "concatenate")
NIL
;; Default upcasing of symbol names means that the
;; result of 'cat is a symbol named "CAT", which is not
;; in "concatenate".
CL-USER> (substringp 'cat "concatenate")
NIL
;; You can test the characters with CHAR-EQUAL, which
;; is case insensitive, in which case "CAT" is in
;; "concatenate".
CL-USER> (substringp 'cat "concatenate" :test 'char-equal)
3
使用递归
您的代码以及 uselpa 在另一个答案中显示的代码本质上更具递归性。这本身不是问题,但 Common Lisp 中的递归字符串处理容易出现一些陷阱。使用 subseq 制作大量新字符串效率低下,因此 Common Lisp 中的许多序列函数采用 :start 和 :end 参数,或者对于采用两个序列的函数,:start1、:end1、:start2 和 :end2论据。通过使用这些,您可以递归并将 indices 更改为字符串,而不是创建全新的字符串。例如,string= 可让您比较两个字符串。
;; "toc" is in both "octocat" and "toccata"
CL-USER> (string= "octocat" "toccata" :start1 2 :end1 5 :end2 3)
T
使用这些类型的函数需要一点小心,以确保您不提供任何超出范围的索引,但这还不错,并且您最终不会复制任何字符串。下面是 substringp 的一个版本,它接受这些开始和结束参数,并使用本地递归函数进行实际处理。
(defun substringp (string1 string2
&key
(start1 0) (end1 nil)
(start2 0) (end2 nil))
"Returns the index of the first occurence of the substring of
STRING1 bounded by START1 and END1 within the substring of STRING2
bounded by START2 and END2, or NIL if the string does not appear. The
index is a position within STRING2 as a whole."
;; First, compute the actual strings designated by STRING1 and
;; STRING2, and the values for END1 and END2, which default to the
;; length of the respective strings. Also get the length of the
;; substring in STRING1 that we're looking for. This is done just
;; once. The actual recursive portion is handled by the local
;; function %SUBSTRINGP.
(let* ((string1 (string string1))
(string2 (string string2))
(end1 (or end1 (length string1)))
(end2 (or end2 (length string2)))
(len1 (- end1 start1)))
(labels ((%substringp (start2 &aux (end2-curr (+ start2 len1)))
(cond
;; If end2-curr is past end2, then we're done, and
;; the string was not found.
((not (< end2-curr end2)) nil)
;; Otherwise, check whether the substrings match. If
;; they do, return the current start2, which is the
;; index of the substring within string2.
((string= string1 string2
:start1 start1 :end1 end1
:start2 start2 :end2 end2-curr)
start2)
;; If that doesn't match, then recurse, starting one
;; character farther into string2.
(t (%substringp (1+ start2))))))
(%substringp start2))))