string - 验证一个字符串是否包含在 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) (string2)) (string1 (string-include string1 (cdr string2))))
    ((string-include  string1 (cdr string2)) ) )

最佳答案

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

在你的问题中,你使用了这个例子:

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

假设您要返回符号 nilghf,如果您想检查是否一个字符串包含子字符串 NIL。例如,使用这种方法,您将拥有:

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

返回 nil 是因为 "NIL""VANILLA" 中,因为它不是?这是模棱两可的,你无法分辨。相反,您可以返回实际的字符串,因为 string "NIL" 是一个真值。更妙的是,如果您返回字符串的 索引,那么您会发现第一个字符串出现在另一个字符串中的位置。例如,这就是内置函数 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),因此您可以将结果用作 bool 值或索引(只要您检查它不是)。以下是一些示例:

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争论。通过使用这些,您可以递归并将 索引 更改为字符串,而不是创建全新的字符串。例如,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))))

关于string - 验证一个字符串是否包含在 Lisp 中的另一个字符串中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620352/

相关文章:

c++ - 用另一个子字符串 C++ 替换子字符串

algorithm - 区分普通 lisp 中的列表和原子

recursion - 使用 CLISP 和递归

c - 无法将指向字符的指针分配给字符数组

c - 在 C 中解析 URL

list - 如何将列表转换为 LISP 中的集合?

lisp - lisp 中的非阻塞输入

coding-style - Lisp:从列表中删除尾随 nil 的优雅方法? (审查)

lisp - 取消列出列表 - 常见的 lisp

php - 如何从列表中获取最不同的字符串