2010-11-01 151 views
0
(define *graph* (read (open-input-file "test.sxml"))) 

(define get 
    (lambda (l) 
    (cond ((null? l) '()) 
      ((equal? 'opm:artifacts (car l)) l) 
      (else (get (cdr l)))))) 

(get *graph*) 

我有這個遞歸函數遍歷列表並返回以「opm:artifacts」開頭的列表的其餘部分。通過列表檢索其他列表

它適用於其他列表。 例如,它適用於列表(1 2 3 4);當我呼叫該功能時, (get 2)返回(2 3 4)

test.sxml是一個表。我用list?進行了檢查。

+0

請檢查您的事實。您的演示文稿不一致。 '(get 2)'肯定不適用於你顯示的定義。 – Svante 2010-11-01 20:55:18

回答

0
(define (get l) 
    (match l 
    [(? null?) '()] 
    [(list 'opm:artifacts _ ...) l] 
    [(list _ rs ...) (get rs)])) 
+0

你在哪裏得到'match'? – erjiang 2010-11-02 16:52:14

+0

這實際上並沒有什麼不同,除非你使用模式匹配來定義它。 – 2010-11-07 16:15:30

0
(define (get mat ls*) 
    (define (get* ls) 
    (cond ((null? ls) '()) 
      ((and (list? (car ls)) (not (null? (car ls)))) 
      (if (equal? mat (caar ls)) 
       (car ls) 
       (let ((sub-result (get* (car ls)))) 
       (if (null? sub-result) 
        (get* (cdr ls)) 
        sub-result)))) 

      (else (get* (cdr ls))))) 
    (let ((result (get* ls*))) 
    (if (null? result) 
     '() 
     (cdr result)))) 

(get 'b '(a (b c d) e)) ;-> '(c d) 
(get 'b '((a (b c d) e))) ;-> '(c d) 
(get '() '(4 6() (2()) (()()()))) ;-> '(()()) 

我也一般化,所以你可以用手在你想要它匹配反對什麼。

+0

非常感謝Tyr! :) – pantelis 2010-11-08 13:17:36

+0

繼續,然後接受它,所以問題被關閉 – 2010-11-08 22:11:58