[Solved] creating assoc function in lisp that will find value from a-list


The most basic lisp operations are list eaters:

(defun some-function (list-to-consume perhaps-additional-args)
  (cond ((endp list-to-consume) <end-of-list-expression>)
        ((<predicate> list-to-consume perhaps-additional-args)
         <result-expression>)
        (t (some-function (cdr list-to-consume) perhaps-additional-args))))

Examples:

;; The predicate is the endp expression
(defun mylength (list &optional (len 0))
  (cond ((endp list) len)
        (t (mylength (cdr list) (1+ len)))))

;; A member function
(defun mymember (element list)
  (cond ((endp list) nil)
        ((equal (car list) element) list)
        (t (mymember element (cdr list)))))

;; Exchange an element with another in a list
;; notice how this builds up a list recursively
(defun exchange (list element replacement)
  (cond ((endp list) nil)
        ((equal (car list) element) (cons replacement (exchange (cdr list) element replacement)))
        (t (cons (car list) (exchange (cdr list) element replacement)))))

There are other more advanced ways to replace most list eaters with loop or higher order functions like mapcar, map and reduce, but if you are learning LISP you probably should be familiar with the list eaters first.

2

solved creating assoc function in lisp that will find value from a-list