Created
December 29, 2017 04:20
-
-
Save bigos/9cb9b0abb053496ee74e3272a2d8025a to your computer and use it in GitHub Desktop.
Experiment with tree traversal in Lisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; tree traversal | |
(defparameter tr '((s | |
(num | |
(dig | |
(1)) | |
(diga | |
(da | |
(2a)) | |
(da | |
(2b)))) | |
(op | |
(+)) | |
(num | |
(dig | |
(3)))))) | |
;;; bottom up - (bu tr) | |
(defun bu(tr) | |
(if (consp (car tr)) | |
(bu (car tr) )) | |
(if (consp (cdr tr)) | |
(progn | |
(when (consp (car tr)) | |
(format t "dt ~a ~%" (car tr))) | |
(bu (cdr tr))) | |
(progn | |
(when (consp (car tr)) | |
(format t "de ~a ~%" (car tr)))))) | |
;;; top down - (td (car tr)) | |
(defun td (tr) | |
(if (consp (car tr)) | |
(td (car tr))) | |
(if (consp (cdr tr)) | |
(progn | |
(unless (consp (car tr)) | |
(format t "dt ~a~%" tr)) | |
(td (cdr tr))) | |
(progn | |
(unless (consp (car tr)) | |
(format t "de ~a~%" tr))))) | |
(bu tr) | |
(format t "^-bottom-up top-down-v~%") | |
(td (car tr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment