Chris Chambers
Chris Chambers

Reputation: 1365

Map not properly applying function to list of lists

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.

Upvotes: 0

Views: 57

Answers (1)

user4815162342
user4815162342

Reputation: 155046

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.

Upvotes: 2

Related Questions