Advertisement
Nuparu00

No tree - no path

Dec 3rd, 2021
2,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.66 KB | None | 0 0
  1. (defun find-path (tree elem)
  2.   (labels ((layer (list) (cond ((null list) nil) ;;This goes through a list of children and if and cheks whether one of them returns !nil
  3.                                (t (let ((next (find-path (car list) elem)))
  4.                                     (if (eql next nil) (layer (cdr list)) next))))))
  5.     (cond ((null tree) nil) ;;No tree - no path
  6.           ((eql (car tree) elem) (list (car tree))) ;;We are done here
  7.           (t (let ((result (layer (cdr tree)))) ;;Let's save the result so we don't have to call this twice
  8.                (if (eql result nil) nil (cons (car tree) result))))))) ;;If it is not nil, we are on the way home
  9.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement