Commit 17a5d9d6 authored by Juergen Nickelsen's avatar Juergen Nickelsen
Browse files

assoc, assq, sassoc, sassq; tests 030 still fail

parent 38dfa894
Loading
Loading
Loading
Loading
+54 −22
Original line number Diff line number Diff line
@@ -617,3 +617,35 @@ list as appropriate."
    (+ (fib (- n 1))
       (fib (- n 2)))))

(defun assoc (item alist)
  "return the pair of ALIST whose car is equal to ITEM, or nil"
  (if (null alist)
      nil
    (if (equal item (caar alist))
        (car alist)
      (assoc item (cdr alist)))))

(defun assq (item alist)
  "return the pair of ALIST whose car is eq to ITEM, or nil"
  (if (null alist)
      nil
    (if (eq item (caar alist))
        (car alist)
      (assoc item (cdr alist)))))

(defun sassoc (item alist func)
    "return the pair of ALIST whose car is equal to ITEM, or call FUNC"
  (if (null alist)
      (func)
    (if (equal item (caar alist))
        (car alist)
      (assoc item (cdr alist)))))

(defun sassq (item alist func)
    "return the pair of ALIST whose car is equal to ITEM, or call FUNC"
  (if (null alist)
      (func)
    (if (eq item (caar alist))
        (car alist)
      (assoc item (cdr alist)))))

tests/030-assoc.lisp

0 → 100644
+37 −0
Original line number Diff line number Diff line

(defvar the-alist '((3 . 4)
                    (7 . 5)
                    (lala . humdi)
                    (10 . 11)
                    ((1 2 3) . 12)
                    ("hudi" . :rudi)))

(testcmp "assoc 0" '(assoc 'lala nil) nil)
(testcmp "assoc 1" '(errset (assoc 'lala '(4))) nil)
(testcmp "assoc 2" '(assoc 'lala the-alist) '(lala . humdi))
(testcmp "assoc 3" '(assoc '(1 2 3) the-alist) '((1 2 3) . 12))
(testcmp "assoc 4" '(assoc '(1 2 5) the-alist) nil)
(testcmp "assoc 5" '(assoc 10 the-alist) '(10 . 11))

(testcmp "assq 0" '(assq 'lala nil) nil)
(testcmp "assq 1" '(errset (assq 'lala '(4))) nil)
(testcmp "assq 2" '(assq 'lala the-alist) '(lala . humdi))
(testcmp "assq 3" '(assq '(1 2 3) the-alist) nil)
(testcmp "assq 4" '(assq '(1 2 5) the-alist) nil)

(defun default () 'this)
(defvar the-function 'default)

(testcmp "sassoc 0" '(sassoc 'lala nil #'default) 'this)
(testcmp "sassoc 1" '(errset (sassoc 'lala '(4) #'default)) nil)
(testcmp "sassoc 2" '(sassoc 'lala the-alist #'default) '(lala . humdi))
(testcmp "sassoc 3" '(sassoc '(1 2 3) the-alist #'default) '((1 2 3) . 12))
(testcmp "sassoc 4" '(sassoc '(1 2 5) the-alist #'default) 'this)
(testcmp "sassoc 5" '(sassoc 10 the-alist 'default) '(10 . 11))

(testcmp "sassq 0" '(sassq 'lala nil #'default) 'this)
(testcmp "sassq 1" '(errset (sassq 'lala '(4) #'default)) nil)
(testcmp "sassq 2" '(sassq 'lala the-alist #'default) '(lala . humdi))
(testcmp "sassq 3" '(sassq '(1 2 3) the-alist #'default) 'this)
(testcmp "sassq 4" '(sassq '(1 2 5) the-alist the-function) 'this)