开发者

go through an sxml file in schme

im trying to load a sxml file... i manage to do that in scheme. now i want to go through it using recursion and located items that i want. my code is like this,

(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)))))

but it seems that it goes through only the first line of the sxml fil开发者_开发问答e. how can i make it go through all the file until the end?


1) You need to search through all of the sublists of the structure. Your code right now only looks at the top-most elements. 2) You usually don't want to have multiple statements in a row (like your two if statements) 3) You probably want to look for symbols, not strings, in your SXML file. Regardless, you must use equal? to compare strings.

Thus, your function becomes

(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))))))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜