通过列表检索其他列表

问题描述:

(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

(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

(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