0
(define (inOrderTraverse root)
  (cond
    [(not (list? root)) (display root)]
    [else (
      (inOrderTraverse (getLeftChild root))
      (display (getValue root))
      (inOrderTraverse (getRightChild root))
    )]
  )
)

This gives me: enter image description here

I tried replacing cond with if, but that didn't work. I looked up online for similar approaches and found:

(define (inorder ~node func)
    (unless (empty? ~node)
      (inorder (node-left ~node) func)
      (print (node-val ~node)) (print ", ")
      (inorder (node-right ~node) func)))

from a gist of github/arjunvasan. After editing their code to fit mine, it worked perfectly, with the only difference being that they used unless instead of cond. Why does unless work better here? I checked the racket docs on this keyword but still haven't found it.

Shawn
  • 47,241
  • 3
  • 26
  • 60
  • Your code is trying to use whatever `(inOrderTraverse (getLeftChild root))` returns as a function. Gotta pay more attention to your parens. Better code formatting would help. – Shawn Nov 11 '22 at 00:06
  • The problem is that you wrapped the `else` body in parentheses. Parentheses are not used for indicating block structure in Scheme. – molbdnilo Nov 11 '22 at 06:34

0 Answers0