Scheme 在schme中浏览一个sxml文件
我正在尝试加载一个sxml文件。。。我在计划中设法做到了这一点。现在,我想使用递归遍历它,并找到我想要的项。我的代码是这样的Scheme 在schme中浏览一个sxml文件,scheme,Scheme,我正在尝试加载一个sxml文件。。。我在计划中设法做到了这一点。现在,我想使用递归遍历它,并找到我想要的项。我的代码是这样的 (define file (read(open-input-file "test1.sxml"))) (define myfunc (lambda (S) (if (eq? "foo" (car S)) (display "found\n") (display "not found\n") ) (if (nu
(define file (read(open-input-file "test1.sxml")))
(define myfunc
(lambda (S)
(if (eq? "foo" (car S))
(display "found\n")
(display "not found\n")
)
(if (null? (cdr S))
(display "\n")
(myfunc(cdr S)))))
但它似乎只通过了sxml文件的第一行。我如何才能让它遍历所有文件直到结束?1)您需要搜索结构的所有子列表。您的代码现在只查看最上面的元素。
2) 您通常不希望在一行中有多个语句(如两个if
语句)
3) 您可能希望在SXML文件中查找符号,而不是字符串。无论如何,您必须使用equal?
来比较字符串
因此,您的函数变得
(define myfunc
(lambda (S)
(cond
((null? S) #f)
((pair? (car S)) (or (myfunc (car S)) (myfunc (cdr S)))) ; search on sublists
((equal? "foo" (car S)) #t) ; if found, return true
(else (myfunc (cdr S))))))
我在我的sxml文件上尝试了你的代码,但它返回了一个错误的值