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

时间:2016-01-05 20:08:08

标签: 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) (string2)) (string1 (string-include string1 (cdr string2))))
    ((string-include  string1 (cdr string2)) ) )

2 个答案:

答案 0 :(得分:4)

返回索引或子字符串,而不是符号

在您的问题中,您使用了此示例:

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

假设您正在返回符号 nil ghf ,如果您想检查是否有疑问,则会遇到歧义字符串包含子字符串 NIL 。例如,通过这种方法,您将拥有:

(string-include 'nil 'vanilla) => nil

是否返回 nil ,因为“NIL”位于“VANILLA”,因为它不是?这是模棱两可的,你无法分辨。相反,您可以返回实际字符​​串,因为字符串 “NIL”是一个真值。更好的是,如果你返回字符串的 index ,那么你会发现第一个字符串出现的另一个字符串中的 where 。例如,这就是内置函数搜索的行为方式。

直接使用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”。最后,由于这会返回搜索的结果,因此它会为您带来双重任务:如果发生,则返回第一次出现的索引,并且为零否则。请记住,除 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))))

答案 1 :(得分:3)

根据您的代码判断,您正在寻找的是这样的:

(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

这有效,但效率低,而不是惯用的Common Lisp。只需确保您实际传递字符串而不是示例中的符号:

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

当然,Joshua's answer是推荐的解决方案。

编辑

添加了一个适用于符号和字符串的版本(但无论如何都会返回字符串)。我借此机会列出了约书亚的一个建议:

(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"