Search code examples
lispelispcommon-lisp

Get element from list of list in lisp


I am a beginner with lisp. I manipulate list of list: ((name1, second) (name2, second2))

The goal of my function is to get the second element of the list that have name as it first node.

For example: my list is: ((name1, second1) (name2, second2)) getelement list name1 should return second1.

(defun getelement (list name)
  (if (eq list '())
    (if (string= (caar list) name)
      (car (car (cdr list)))
      (getelement (cdr list) name)
    )
    ()
  )
)

But I get this error. I really don't understand what is happening with my code. I tried to put ' before expressions...

Error: The variable LIST is unbound.
Fast links are on: do (si::use-fast-links nil) for debugging
Error signalled by IF.
Backtrace: IF

Solution

  •  (defun get-element (list name)
        (cadr (assoc name list :test #'string=)))