tags:

views:

28

answers:

1

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 file. how can i make it go through all the file until the end?

A: 

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))))))
erjiang
i tried your code on my sxml file but it returns me a false value
pantelis