trec in On Lisp

On page 74, there is "The second arg to trec should be a function of three arguments: the current object and the two

recursers. " For CLISP, two arguments are enough. I am using CLISP 2.44.1 on Ubuntu 10.04. Here is the code:

 

(defun trec (rec &optional (base #'identity))
  (labels
    ((self (tree)
           (if (atom tree)
             (if (functionp base)
               (funcall base tree)
               base)
             (funcall rec 
                      #'(lambda ()
                          (self (car tree)))
                      #'(lambda ()
                          (if (cdr tree)
                            (self (cdr tree))))))))
    #'self))

(defun mklist (obj)
  (if (listp obj) obj (list obj)))

(setq find-odd
      (trec #'(lambda ( l r) (or (funcall l) (funcall r)))
            #'(lambda (tree) (and (oddp tree) tree))))

(setq flatten
  (trec #'(lambda (l r) (nconc (funcall l) (funcall r)))
        #'mklist))

(funcall find-odd '(2 (4 (5)) 7))
(funcall flatten '(2 (4 (5)) 7))
 

 

 

你可能感兴趣的:(lisp)