lisp 習題 our-union

babyyellow發表於2012-08-07
            寫一個保留原本列表中元素順序的 union 版本:
 
  
> (new-union '(a b c) '(b a d))
(A B C D)


CL-USER> (defun our-union (fi la)
       (and (listp fi)
        (listp la)
        (let (( res (reverse fi)))
             (dolist (elt la)
               (pushnew elt res))
             (reverse res))))
STYLE-WARNING: redefining COMMON-LISP-USER::OUR-UNION in DEFUN
OUR-UNION
CL-USER> (our-union '(a b c) '(b a d))
(A B C D)
CL-USER> (our-union 'a '(b a d))
NIL
CL-USER> (our-union '(a b) 'a)
NIL
CL-USER> (our-union '(a b) 'a)
NIL


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/133735/viewspace-740221/,如需轉載,請註明出處,否則將追究法律責任。

相關文章