2009-09-09 3 views
6

방금 ​​LISP를 사용하기 시작했습니다. 놀라운 학습 곡선 (저는 이맥스 초보자입니다)과 함께 지금까지 재미있었습니다.subseq (LISP)의 간단한 문제

어쨌든 다음 소스 코드의 구문을 구문 분석하는 다음 코드에 대해 바보 같은 문제가 있습니다. 누군가 의견을 말하고 해결책을 제안하면 많은 도움이됩니다.

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

나는 실제 결과가

"#include <stdio.h>" 

어떤 생각입니다 그러나 마지막 줄

"<stdio.h>" 

를 반환 할 것으로 예상?

답변

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

* 이마를 때리며, 내 논리는 단지 명백한 잘못이었습니다 - 우리는 2 일차가 어떻게되는지를 볼 것입니다 :-) – Justicle

1

나는 replace-in-string을 훨씬 쉽게 발견합니다. 이름에서 알 수 있듯이 코드에 대한

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

include-start, 경기의 시작을 반환합니다. 당신은 아마 인 include-end를 찾고 있습니다 (+ (include-start ....) (length ....))

+0

어디 '대체 된 문자열'- 그것은 표준 기능입니다? 다윈에 Closure Common Lisp을 사용하고 있습니다. – Justicle

+0

오, 당신이 elisp을 사용하고 있다고 생각했습니다. –

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