From: Owen Rodley Date: Sat, 4 May 2013 06:39:05 +0000 (+1200) Subject: ADJOIN X-Git-Url: http://repo.macrolet.net/gitweb/?a=commitdiff_plain;h=a14d0ed62b18400b33805670b900db6a63899796;p=jscl.git ADJOIN --- diff --git a/src/list.lisp b/src/list.lisp index 0bd4846..7a4ca6d 100644 --- a/src/list.lisp +++ b/src/list.lisp @@ -265,6 +265,11 @@ (rplacd 2nd 3rd))) +(defun adjoin (item list &key (test #'eql) (key #'identity)) + (if (member item list :key key :test test) + list + (cons item list))) + (defun intersection (list1 list2 &key (test #'eql) (key #'identity)) (let ((new-list ())) (dolist (x list1) diff --git a/src/toplevel.lisp b/src/toplevel.lisp index 35edf4f..9261925 100644 --- a/src/toplevel.lisp +++ b/src/toplevel.lisp @@ -49,8 +49,8 @@ (values-list /)) (export '(&body &key &optional &rest * ** *** *gensym-counter* *package* + ++ - +++ - / // /// 1+ 1- < <= = = > >= acons and append apply aref arrayp - assoc atom block boundp butlast cadar caaar caadr cdaar cdadr + +++ - / // /// 1+ 1- < <= = = > >= acons adjoin and append apply aref + arrayp assoc atom block boundp butlast cadar caaar caadr cdaar cdadr cddar caaaar caaadr caadar caaddr cadaar cadadr caddar cdaaar cdaadr cdadar cdaddr cddaar cddadr cdddar cddddr caar cadddr caddr cadr car car case catch cdar cdddr cddr cdr cdr char diff --git a/tests/list.lisp b/tests/list.lisp index ad5eb62..2f1b57c 100644 --- a/tests/list.lisp +++ b/tests/list.lisp @@ -69,6 +69,11 @@ (test (equal (member 4 '((1 . 2) (3 . 4)) :key #'cdr) '((3 . 4)))) (test (member '(2) '((1) (2) (3)) :test #'equal)) +; ADJOIN +(test (equal (adjoin 1 '(2 3)) '(1 2 3))) +(test (equal (adjoin 1 '(1 2 3)) '(1 2 3))) +(test (equal (adjoin '(1) '((1) (2)) :test #'equal) '((1) (2)))) + ; INTERSECTION (test (equal (intersection '(1 2) '(2 3)) '(2))) (test (not (intersection '(1 2 3) '(4 5 6))))