(defun heapify-down (heap-id
index)
(if
(is-leaf heap-id
index)
nil
(let ((left (left-child index))
(right (right-child index))
(heap-array (heap-actual-heap heap-id)))
(cond
((null (aref heap-array
left))
(if (= (find-largest heap-id
index
right)
index)
(swap heap-array
index
right))
nil)
((null (aref heap-array
right))
(if (= (find-largest heap-id
index
left)
index)
(swap heap-array
index
left))
nil)
((and (= (find-largest heap-id
index
left)
left)
(= (find-largest heap-id
index
right)
right))
nil)
(t
(let (new-index (find-smallest heap-id
left
right))
(swap heap-array
index
new-index)
(heapify-down heap-id
new-index)))))))
So i just wanna know if i'm formatting the right way. i looked some online guides on the topic but i'm not really sure that i'm doing it the best way, especially because my teacher is really strict about the topic.
There is no single parenthesis on a line or various other common problems, and it looks like the forms are aligned correctly, so it is already readable; you tend to add a lot of newlines for no particular reason I think (e.g. after the if?): Common Lisp is often a little more compact vertically. Also note that you have macros for the cases where the
ifhas one branch that is nil, namelywhenandunless.I would rewrite it as follows, with small helper functions to make things a little clearer hopefully. You should try to add comments too.