2009-09-09 20 views
6

CのバックグラウンドからLISPを使い始めたばかりですが、これまでのところ楽しいですが、信じられないほどの学習曲線があります(私はemacs初心者です)。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

どこにある「を置換-に文字列」 - それは標準機能ですか?私はDarwinにClosure Common Lispを使用しています。 – Justicle

+0

ああ、あなたはelispを使っていると思った。 –

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