Yoa*_*n15 2 lisp string common-lisp
我正在尝试编写一个函数来验证一个字符串是否包含在 Lisp 中的另一个字符串中,但我不能
例如 :
(string-include 'abd 'abbbe) => nil
(string-include 'ghf 'dghfd) => ghf
Run Code Online (Sandbox Code Playgroud)
这是我的功能:
(defun string-include (string1 string2)
(cond
((not string1) 0)
((not string2) 0)
((.... (string1) (string2)) (string1 (string-include string1 (cdr string2))))
((string-include string1 (cdr string2)) ) )
Run Code Online (Sandbox Code Playgroud)
在您的问题中,您使用了以下示例:
Run Code Online (Sandbox Code Playgroud)(string-include 'abd 'abbbe) => nil (string-include 'ghf 'dghfd) => ghf
假设您要返回符号 nil和ghf,如果您想检查字符串是否包含子字符串NIL,您将遇到歧义。例如,使用这种方法,您将拥有:
(string-include 'nil 'vanilla) => nil
Run Code Online (Sandbox Code Playgroud)
返回nil是否因为"NIL"在"VANILLA" 中,因为它不是?这是模棱两可的,你无法分辨。相反,您可以返回实际字符串,因为字符串 “NIL”是一个真值。更好的是,如果您返回字符串的索引,那么您会发现第一个字符串出现在另一个字符串中的哪个位置。例如,这就是内置函数搜索的行为方式。
您可以在搜索方面实现这一点:
(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))
Run Code Online (Sandbox Code Playgroud)
请注意使用string函数将字符串指示符(字符、字符串和符号)转换为它们指定的字符串。请记住,使用标准设置,阅读器会将符号的名称大写,因此符号cat指定字符串"CAT"。最后,由于这会返回search的结果,因此它对您有双重作用:如果出现,则返回第一次出现的索引,否则返回nil。请记住,除nil之外的所有内容都是真值(甚至 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
Run Code Online (Sandbox Code Playgroud)
您的代码以及 uselpa 在另一个答案中显示的代码本质上更具递归性。这本身不是问题,但 Common Lisp 中的递归字符串处理容易出现一些陷阱。使用subseq 生成大量新字符串是低效的,因此 Common Lisp 中的许多序列函数采用:start和:end参数,或者在采用两个序列的函数的情况下,:start1、:end1、:start2和: end2参数。通过使用这些,您可以递归并将索引更改为字符串,而不是创建全新的字符串。例如,字符串= 让你比较两个字符串。
;; "toc" is in both "octocat" and "toccata"
CL-USER> (string= "octocat" "toccata" :start1 2 :end1 5 :end2 3)
T
Run Code Online (Sandbox Code Playgroud)
使用这些类型的函数需要小心谨慎,以确保您不会提供任何超出范围的索引,但这还不错,并且您最终不会复制任何字符串。这是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))))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2539 次 |
| 最近记录: |