taruss
10/13/2015 6:09:00 PM
On Tuesday, October 13, 2015 at 9:46:42 AM UTC-7, Anoop GR wrote:
> Thank you Antsan and Barry,
> code fixed: yeah!
>
> (defun copy-trt (trt0)
> (cond ((trt-p trt0)
> (make-trt :elt (trt-elt trt0)
> :left (if (null (trt-left trt0))
> nil
> (copy-trt (trt-left trt0)))
> :middle (if (null (trt-middle trt0))
> nil
> (copy-trt (trt-middle trt0)))
> :left (if (null (trt-right trt0))
> nil
> (copy-trt (trt-right trt0)))))))
>
> Kaz, thank you for the analogy and predicate suppression keyword
Just a couple of style notes on this function.
(1) Instead of (IF (NULL (F ...)) NIL ...) it is often clearer to write
(WHEN (F ...) ...) without the NULL predicate if you will just return
NIL when the predicate is false.
(2) A COND (or IF statement, for that matter) with only one clause is also
better replaced by a WHEN or UNLESS form. That gives a more stream-lined
result:
(defun copy-trt (trt0)
(when (trt-p trt0)
(make-trt :elt (trt-elt trt0)
:left (when (trt-left trt0)
(copy-trt (trt-left trt0)))
:middle (when (trt-middle trt0)
(copy-trt (trt-middle trt0)))
:left (when (trt-right trt0)
(copy-trt (trt-right trt0))))))
(3) Although not as robust, since the COPY-TRT form returns NIL if given NIL
as its input, you could remove the test for copying the children. But as
noted, that will make the function less robust for future changes.