Reputation: 23
Hi I am trying to transfer a binary tree to a list but crossing it in post-order.
(define (postorden arbol)
(if (null? arbol)
('()) (append (append (postorden (car (cdr arbol)))
(postorden (car (cddr arbol))))
(list (car arbol)))))
But I get this error:
cdr: contract violation expected: pair? given: 'null
But for example with this tree: '((1 2) ((7 10) ((2 4) null null)) ((6 8) ((10 13) null null) null))
performed the cdr and get: (((7 10) ((2 4) null null)) ((6 8) ((10 13) null nul) null)) and then I make the car to this outcome: '((7 10) ((2 4) null null)) which if it is a pair. So I do not understand what is wrong.
Thank you in advance.
Upvotes: 1
Views: 292
Reputation: 31147
An if
expression has the for (if e0 e1 e2)
where e0,e1,and,e2 are expressions.
In your program you have:
(if e0
('() ...)
e2)
This means that if e0 is true, then ('() ...)
is evaluated.
Here '()
is the empty list also known as null
.
The expression ('() ...)
will attempt to call '()
with the result of ...
,
and thus give an error.
Fix this error, and hopefully your other errors disappears - if not show us what you've and ask again.
Upvotes: 1