subseq(LISP)的简单问题

时间:2009-09-09 07:13:49

标签: string lisp common-lisp

我刚开始使用LISP,来自C的背景。到目前为止它很有趣,虽然有一个令人难以置信的学习曲线(我也是一个emacs新手)。

无论如何,我对以下代码有一个愚蠢的问题来解析来自c source的include语句 - 如果有人可以对此发表评论并建议解决方案,那将会有很大帮助。

(defun include-start ( line )
    (search "#include " line))

(defun get-include( line )
  (let ((s (include-start line)))
    (if (not (eq NIL s))
      (subseq line s (length line)))))

(get-include "#include <stdio.h>")

我希望最后一行返回

"<stdio.h>"

然而实际结果是

"#include <stdio.h>"

有什么想法吗?

3 个答案:

答案 0 :(得分:6)

(defun include-start (line)
  "returns the string position after the '#include ' directive or nil if none"
  (let ((search-string "#include "))
    (when (search search-string line)
      (length search-string))))

(defun get-include (line)
  (let ((s (include-start line)))
    (when s
      (subseq line s))))

答案 1 :(得分:1)

我发现replace-in-string更容易。

(replace-in-string "#include <stdio.h>" "#include +" "")
    => "<stdio.h>"

对于您的代码,include-start会返回匹配的开头,顾名思义。您正在寻找可能include-end

(+ (include-start ....) (length ....))

答案 2 :(得分:1)

(defun get-include (s)
   (subseq s (mismatch "#include " s)))