0
votes

Given the define tree:

(define tree
  '("S" (("-" ("A" 3333) ("A" 4444))
         ("W" (("+" ("R" 0) ("R" 1))
               ("+" ("R" 1) ("R" 2))
               ("+" ("R" 2) ("R" 3))
               ("+" ("R" 3) ("R" 4))
               ("+" ("R" 4) ("R" 5)))
              (("-" ("R" 0) ("R" 1))
               ("-" ("R" 1) ("R" 2))
               ("-" ("R" 2) ("R" 3))   
               ("-" ("R" 3) ("R" 4))
               ("-" ("A" 1000) ("A" 2000)))))))

I am trying to access the values. Doing car and cdr works for the first and getting the rest but when I try to get a specific value like ("A" 1000) I get the error:

cdr: contract violation
expected: pair?
given: '().

I have tried (car (cdr (cdr (cdr '(tree))))), (cdddr tree) but I always get that error. Any helpful tips will be appreciated.

1

1 Answers

1
votes

You must not put tree inside a list: '(tree), that's just a list with the symbol 'tree, with no relationship with the value of the actual variable you defined before. And to access the desired element, do this:

(car (cdr (car (car (cdr tree)))))
=> '("A" 3333)

Also, I'd like to suggest that you rethink the way you're representing and/or traversing the information, as you can see, it's pretty hard to extract data out of such a complex tree...