views:

53

answers:

3

This is my code:

   (define p (read(open-input-file "starbucks4.sxml")))

(define get-artifacts
  (lambda (l)
   (member (list 'opm:artifact) l)))



  (get-artifacts p)

I was told that the member function searches completely throughout a list. In the .sxml document there is a complicated list that has many elements called "opm:artifact" however this method returns #f and no list.

Can anyone see what I am doing wrong?

Sample of .sxml file:

      (opm:account ((ref "detailedAccount")))
   "\n            "
   (opm:label ((value "Provide other Beverage")))
   "\n        ")
  "\n    ")
 "\n    "
 (opm:artifacts
  ()
  "\n        "
  (opm:artifact
   ((id "a1"))
   "\n            "
   (opm:account ((ref "detailedAccount")))
   "\n            "
   (opm:label ((value "order")))
   "\n        ")
  "\n        "
  (opm:artifact
   ((id "a2"))
   "\n            "
   (opm:account ((ref "detailedAccount")))
   "\n            "
   (opm:label ((value "cash")))
   "\n        ")
  "\n        "

I'm trying to look for all the opm:artifacts and the associated data (it's sublists).

+1  A: 

It does search through the whole list, but it doesn't search sublists.

So if your list is actually a nested list and the (opm:artifact) is only in one of its sublists, member won't find it.

Also note that you're looking for the list (opm:artifact), not the symbol opm:artifact or any list containing opm:artifact.

Edit: To search sublists you can do something like this:

(define (deep-search x lst)
  (if (empty? lst)
    #f
    (if (equal? x (car lst))
      #t
      (begin
        (if (list? (car lst))
          (let ((r (deep-search x (car lst))))
               (if r
                 r
                 (deep-search x (cdr lst))))
          (deep-search x (cdr lst)))))))
sepp2k
Thank you! How would I search for the symbol opm:artifact?
Alex
@Alex: `(deep-search 'opm-artifact p)` Note that this will only return true or false. You'd need to modify it a bit if you need more than that.
sepp2k
@Alex: Also note that with the sample file, you showed `p` will only contain `(opm:account ((ref "detailedAccount")))`, as `read` only reads a single form.
sepp2k
Thanks sepp2k, I appreciate you helping me, I'll try and workout what that does exactly. One further question, how would I change the syntax to look for the symbol "opm:artifact" instead of the list?
Alex
@Alex: `'opm:artifact` is the symbol, `'(opm:artifact)` or `(list 'opm:artifact)` is the list.
sepp2k
A: 

The first thing I noticed is that you are giving a list of one element as your first argument to member. Member works like this:

>>> (member 2 '(4 5 2 6 7))
(2 6 7)

Can you give us a sample of what p looks like, and what you want as a result?

BudgieInWA
Thanks! I've edited my original question with some sample of p.
Alex