String 函数,用于验证一个字符串是否包含在Lisp中的另一个字符串中

String 函数,用于验证一个字符串是否包含在Lisp中的另一个字符串中,string,lisp,common-lisp,String,Lisp,Common Lisp,我正在尝试编写一个函数来验证一个字符串是否包含在Lisp中的另一个字符串中,但我不能 例如: (string-include 'abd 'abbbe) => nil (string-include 'ghf 'dghfd) => ghf 以下是我的功能: (defun string-include (string1 string2) (cond ((not string1) 0) ((not string2) 0) ((.... (string1) (

我正在尝试编写一个函数来验证一个字符串是否包含在Lisp中的另一个字符串中,但我不能

例如:

(string-include 'abd 'abbbe) => nil

(string-include 'ghf 'dghfd) => ghf
以下是我的功能:

(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)) ) )
返回索引或子字符串,而不是符号 在您的问题中,您使用了以下示例:

假设返回符号nil和ghf,如果要检查字符串是否包含子字符串nil,就会遇到歧义。例如,通过这种方法,您将拥有:

(string-include 'nil 'vanilla) => nil
它返回nil是因为nil在香草中,因为它不是?这是模棱两可的,你说不出来。相反,您可以返回实际字符串,因为字符串NIL是一个真值。更好的是,如果您返回字符串的索引,那么您就可以找到第一个字符串在另一个字符串中出现的位置。例如,这就是内置函数搜索的行为方式

直接使用搜索 您可以通过以下方式实现这一点:

请注意,该函数用于将字符、字符串和符号转换为它们指定的字符串。请记住,在标准设置中,读取器将符号名称升格,因此符号cat指定字符串cat。最后,由于这将返回搜索结果,因此它对您有双重作用:如果出现,则返回第一次出现的索引,否则返回零。请记住,除nil之外的所有值都是真值,即使是0,因此只要检查结果是否为nil,就可以将其用作布尔值或索引。以下是一些例子:

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在另一个答案中显示的代码在本质上更具递归性。这本身并不是一个问题,但在公共Lisp中递归字符串处理容易出现一些陷阱。使用subseq生成大量新的Sting是低效的,因此在常见的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
使用这些类型的函数需要小心一点,以确保不提供任何超出范围的索引,但也不太糟糕,并且不会复制任何字符串。下面是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))))
返回索引或子字符串,而不是符号 在您的问题中,您使用了以下示例:

假设返回符号nil和ghf,如果要检查字符串是否包含子字符串nil,就会遇到歧义。例如,通过这种方法,您将拥有:

(string-include 'nil 'vanilla) => nil
它返回nil是因为nil在香草中,因为它不是?这是模棱两可的,你说不出来。相反,您可以返回实际字符串,因为字符串NIL是一个真值。更好的是,如果您返回字符串的索引,那么您就可以找到第一个字符串在另一个字符串中出现的位置。例如,这就是内置函数搜索的行为方式

直接使用搜索 您可以通过以下方式实现这一点:

请注意,该函数用于将字符、字符串和符号转换为它们指定的字符串。请记住,在标准设置中,读取器将符号名称升格,因此符号cat指定字符串cat。最后,由于这将返回搜索结果,因此它对您有双重作用:如果出现,则返回第一次出现的索引,否则返回零。请记住,除nil之外的所有值都是真值,即使是0,因此只要检查结果是否为nil,就可以将其用作布尔值或索引。以下是一些例子:

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在另一个答案中显示的代码在本质上更具递归性。这本身并不是一个问题,但在公共Lisp中递归字符串处理容易出现一些陷阱。使用subseq生成大量新的Sting是低效的,因此在常见的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
使用这些类型的函数需要小心一点,以确保不提供任何超出范围的索引,但也不太糟糕,并且不会复制任何字符串。下面是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))))

根据您的代码判断,您要查找的内容如下:

(defun string-include (string1 string2)
  (cond
   ((zerop (length string1)) nil) ; string1 is empty (no need to test it every time)
   ((> (length string1) (length string2)) nil) ; string1 is longer than string2
   ((string= string1 (subseq string2 0 (length string1))) string1) ; string2 starts with string1
   (t (string-include string1 (subseq string2 1))))) ; otherwise shorten string2 by 1 and start over
这是有效的,但它是无效的 高效且不惯用的通用Lisp。只需确保您实际传递的是字符串,而不是示例中的符号:

? (string-include "abd" "abbbe")
NIL
? (string-include "ghf" "dghfd")
"ghf"
当然,这是推荐的解决方案

编辑

添加了一个同时使用符号和字符串但仍返回字符串的版本。我借此机会提出了约书亚的一个建议:

(defun string-include (string1 string2)
  (let* ((string1 (string string1)) (length1 (length string1)))
    (if (zerop length1)
        nil 
        (labels ((sub (s)
                   (cond
                    ((> length1 (length s)) nil)
                    ((string= string1 s :end2 (length string1)) string1)
                    (t (sub (subseq s 1))))))
          (sub (string string2))))))
测试:

? (string-include "abd" "abbbe")
NIL
? (string-include "ghf" "dghfd")
"ghf"
? (string-include 'abd  'abbbe) 
NIL
? (string-include 'ghf  'dghfd) 
"GHF"
? (string-include "ghf" '|dghfd|) 
"ghf"
? (string-include '|ghf|  "dghfd") 
"ghf"

根据您的代码判断,您要查找的内容如下:

(defun string-include (string1 string2)
  (cond
   ((zerop (length string1)) nil) ; string1 is empty (no need to test it every time)
   ((> (length string1) (length string2)) nil) ; string1 is longer than string2
   ((string= string1 (subseq string2 0 (length string1))) string1) ; string2 starts with string1
   (t (string-include string1 (subseq string2 1))))) ; otherwise shorten string2 by 1 and start over
这是可行的,但效率低下,而且不是惯用的通用Lisp。只需确保您实际传递的是字符串,而不是示例中的符号:

? (string-include "abd" "abbbe")
NIL
? (string-include "ghf" "dghfd")
"ghf"
当然,这是推荐的解决方案

编辑

添加了一个同时使用符号和字符串但仍返回字符串的版本。我借此机会提出了约书亚的一个建议:

(defun string-include (string1 string2)
  (let* ((string1 (string string1)) (length1 (length string1)))
    (if (zerop length1)
        nil 
        (labels ((sub (s)
                   (cond
                    ((> length1 (length s)) nil)
                    ((string= string1 s :end2 (length string1)) string1)
                    (t (sub (subseq s 1))))))
          (sub (string string2))))))
测试:

? (string-include "abd" "abbbe")
NIL
? (string-include "ghf" "dghfd")
"ghf"
? (string-include 'abd  'abbbe) 
NIL
? (string-include 'ghf  'dghfd) 
"GHF"
? (string-include "ghf" '|dghfd|) 
"ghf"
? (string-include '|ghf|  "dghfd") 
"ghf"


那么,我如何才能准确地编写我的函数呢?为什么在你的示例中substringp cat concatenate返回3?@yoan15正如我在回答中解释的那样,如果子字符串出现在字符串中,那么toy将返回始终为真值的索引。所以3是cat在concatenate中的索引,这是真的,因为cat在concatenate中。@yoan15看起来您的字符串include函数与我的子字符串p具有几乎相同的签名;我不知道你还需要什么。那么我怎样才能准确地编写我的函数呢?为什么在你的例子中substring p cat concatenate返回3?@yoan15正如我在回答中解释的那样,如果子字符串出现在字符串中,那么toy将得到始终为真值的索引。所以3是cat在concatenate中的索引,这是真的,因为cat在concatenate中。@yoan15看起来您的字符串include函数与我的子字符串p具有几乎相同的签名;我不知道你还需要什么。谢谢你,但这不是问题。有一个命令可以将符号转换为字符串。那么这就是您正在寻找的解决方案吗?许多字符串处理函数,包括,精确地接受start1、start2、end1和end2关键字参数,这样您就不必使用subseq进行大量复制。在这种情况下,将string=string1 subseq string2 0 length string1替换为string=string1 string2:end2 length string1会很有帮助。将这些关键字参数添加到字符串include中也有助于避免最后一种情况下的subseq。另外,第一种情况不是倒过来的吗?如果string1是空字符串,则它是每个字符串的子字符串。例如,字符串include“| |”foo应该生成| |而不是nil,对吗?实际上,这也引发了一个问题,关于包含nil的单词。例如,string include'nil'foo=>nil和string include'nil'vanilla=>nil。@uselpa只是为了好玩,我在我的答案中添加了一个递归版本,该版本使用了start和end关键字参数。谢谢,但对于符号,这不是问题。有一个命令可以将符号转换为字符串。那么这就是您正在寻找的解决方案吗?许多字符串处理函数,包括,精确地接受start1、start2、end1和end2关键字参数,这样您就不必使用subseq进行大量复制。在这种情况下,将string=string1 subseq string2 0 length string1替换为string=string1 string2:end2 length string1会很有帮助。将这些关键字参数添加到字符串include中也有助于避免最后一种情况下的subseq。另外,第一种情况不是倒过来的吗?如果string1是空字符串,则它是每个字符串的子字符串。例如,字符串include“| |”foo应该生成| |而不是nil,对吗?实际上,这也引发了一个问题,关于包含nil的单词。例如,string include'nil'foo=>nil和string include'nil'vanilla=>nil。@uselpa只是为了好玩,我在我的答案中添加了一个递归版本,使用start和end关键字参数。您的函数返回的符号的名称包含在other的名称中,否则返回nil。如果第一个符号为零,您会怎么做。例如,如果你的字符串包含'nil'香草,你会得到nil返回,但你无法判断这是因为nil在香草中还是因为它不是。你的函数返回的符号的名称包含在other的名称中,否则返回nil。如果第一个符号为零,您会怎么做。例如,如果你的字符串中包含“nil”香草,你会得到nil,但你无法判断这是因为nil在香草中还是因为它不是。