0

I am having some trouble with using map.

I am defining binary trees as lists, and I have several functions for working with them. I am supposed to come up with several test cases and then run them. I have several trees predefined. However, to save time, I want to run the test cases as a batch with map.

For example, I have the following trees:

(define ctree0 '()) ;  0 nodes

(define ctree1 '("Apple" () ())) ; 1 node

(define ctree4 '("Candy" ("Artichoke" ("Apple" () ()) ()) ("Doughnut" () ()))) ; 4 nodes

I have the following function:

(define empty?
  (lambda (t)
    (null? t)
    )
  )

To save time, I defined a list of lists:

(define ctrees '(ctree0 ctree1 ctree4))

and then tried to run the following;

(map empty? ctrees)

which produced:

(#f #f #f)

The correct result should be (#t #f #f), and this is what I get when I test them individually).

How can I fix this? I've tried playing with removing the front quote that supressed evaluation, but then I obviously get errors about the list being unable to evaluate.

1 Answer 1

2

To save time, I defined a list of lists:

(define ctrees '(ctree0 ctree1 ctree4))

You didn't; you defined a list of three symbols, ctree0, ctree1, and ctree4. To define a list of lists, you must lose the quotes:

(define ctrees (list ctree0 ctree1 ctree4))

After this change, (map empty? ctrees) produces the expected result.

2
  • Perfect. I knew there was something concerning lists of lists that I wasn't understanding. Thanks! Oct 20, 2013 at 21:39
  • @ChrisChambers Another way to spell this is using the backquote operator, which supports selective evaluation: (define ctrees `(,ctree0 ,ctree1 ,ctree4)) (note , preceding the symbols that need evaluation). Oct 20, 2013 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.