I am currently trying to write a function that finds a value in racket using DrRacket. With some help I came up with this below. However I need someone to explain to me what the difference is between cadr and caddr? Also in DrRacket how would I create a BST? Is it similar to making a list?
(define (find-val bst elt)
(cond ((null? bst) #f)
((< elt (car bst))
(find-val (cadr bst) elt))
((> elt (car bst))
(bst (caddr bst) elt))
((equal? elt (car bst))
#t)))
For the first part of your question,
is equivalent to:
and
is equivalent to:
Did you notice the pattern? each
dbetween thecandris a shorthand for acdr, and eachabetween thecandris a shorthand for acar, in left-to-right order.Regarding the second part of your question, there’s a very detailed explanation of how to represent a BST in the book SICP, section §2.3.3 under the title sets as binary trees. There you’ll find the required procedures for creating and manipulating a tree.