Scheme 浏览列表检索其他列表
我有一个递归函数,它遍历列表并返回以“opm:artifacts”开头的列表的其余部分 它适用于其他列表。 例如,它适用于列表Scheme 浏览列表检索其他列表,scheme,Scheme,我有一个递归函数,它遍历列表并返回以“opm:artifacts”开头的列表的其余部分 它适用于其他列表。 例如,它适用于列表(1 2 3 4);当我调用函数时, (get 2)返回(2 3 4) test.sxml是一个列表。我用列表?检查了它 (define *graph* (read (open-input-file "test.sxml"))) (define get (lambda (l) (cond ((null? l) '()) ((equal?
(1 2 3 4)
;当我调用函数时,
(get 2)
返回(2 3 4)
test.sxml
是一个列表。我用列表?
检查了它
(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*)
我还对它进行了概括,以便您可以提交您希望它与之匹配的内容。请检查您的事实。你的陈述前后不一致
(get 2)
当然不能与您显示的定义一起使用。您从哪里获得匹配
?除了使用模式匹配来定义它之外,实际上看起来没有任何不同。
(define (get l)
(match l
[(? null?) '()]
[(list 'opm:artifacts _ ...) l]
[(list _ rs ...) (get rs)]))
(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 ()) (() () ()))) ;-> '(() ())