从lisp中的列表列表中获取元素

从lisp中的列表列表中获取元素,lisp,elisp,common-lisp,Lisp,Elisp,Common Lisp,我是一个口齿不清的初学者。 我操纵列表的列表: ((姓名1,第二)(姓名2,第二)) 我的函数的目标是获取列表中的第二个元素,该元素的名称作为第一个节点 例如: 我的名单是:((姓名1,第二名)(姓名2,第二名)) getelement列表名称1应返回second1 (defun getelement (list name) (if (eq list '()) (if (string= (caar list) name) (car (car (cdr list)))

我是一个口齿不清的初学者。 我操纵列表的列表: ((姓名1,第二)(姓名2,第二))

我的函数的目标是获取列表中的第二个元素,该元素的名称作为第一个节点

例如: 我的名单是:((姓名1,第二名)(姓名2,第二名)) getelement列表名称1应返回second1

(defun getelement (list name)
  (if (eq list '())
    (if (string= (caar list) name)
      (car (car (cdr list)))
      (getelement (cdr list) name)
    )
    ()
  )
)
但我得到了这个错误。我真的不明白我的代码发生了什么。我试着在表达之前加上

Error: The variable LIST is unbound.
Fast links are on: do (si::use-fast-links nil) for debugging
Error signalled by IF.
Backtrace: IF
  • if子句的顺序错误
  • 当字符串匹配时,您将使用下一个元素(cdr)而不是匹配的元素(car)
  • 这应该起作用:

    (defun getelement (list name)
         (if (eq list '()) ;end of list,...
             '() ;return empty list
             (if (string= (caar list) name) ;matches,
                 (car (car list))  ;take the matching element
                 (getelement (cdr list) name))))
    
  • if子句的顺序错误
  • 当字符串匹配时,您将使用下一个元素(cdr)而不是匹配的元素(car)
  • 这应该起作用:

    (defun getelement (list name)
         (if (eq list '()) ;end of list,...
             '() ;return empty list
             (if (string= (caar list) name) ;matches,
                 (car (car list))  ;take the matching element
                 (getelement (cdr list) name))))
    

    @比尔:对不起,我更改了变量的名称,但是忘记了这个。nodelist是list(参数)。当您使用正确的变量名时,它是否会给出错误消息?如果是:使用什么lisp,你如何调用你的函数?@Bill抱歉,我更改了变量的名称,但忘记了这个。nodelist是list(参数)。当您使用正确的变量名时,它是否会给出错误消息?如果是这样:使用什么lisp,如何调用函数?函数应返回匹配元素后面的元素,即
    (car(cdr(car list))
    ,或更好的
    (第二个(第一个列表))
    。函数应返回匹配元素后面的元素,即
    (car(cdr(car list))
    ,或更好的
    (第二个(第一个列表))