Lisp 如何在一秒钟内不归还任何东西
因此,我在lisp中创建这个函数,在Lisp 如何在一秒钟内不归还任何东西,lisp,common-lisp,Lisp,Common Lisp,因此,我在lisp中创建这个函数,在cond部分,基本上如果满足条件,我将返回一个包含2个值的列表,如果不满足条件,我将不返回任何内容!这是: (defun lista-dos-aprovados (turma) (mapcar (lambda (aluno) (cond ((> (media-notas (notas aluno)) 9.5) (list (first aluno) (second aluno)))
cond
部分,基本上如果满足条件,我将返回一个包含2个值的列表,如果不满足条件,我将不返回任何内容!这是:
(defun lista-dos-aprovados (turma)
(mapcar (lambda (aluno)
(cond ((> (media-notas (notas aluno)) 9.5)
(list (first aluno) (second aluno)))
(t nil)))
turma))
名字是葡萄牙语的,但我认为这在这里并不重要。我想做的是,当代码到达
(t nil)
部分时,我不希望它在我的列表中写入nil
。我尝试不使用T
条件,或者在T
之后将其保留为空,但它始终写入NIL
您可以在mapcar
的结果中删除NIL
,如:
(defun lista-dos-aprovados (turma)
(remove nil
(mapcar (lambda (aluno)
(cond ((> (media-notas (notas aluno)) 9.5)
(list (first aluno) (second aluno)))
(t nil)))
turma)))
请注意,您可以将函数简化为:
(defun lista-dos-aprovados (turma)
(remove nil
(mapcar (lambda (aluno)
(when (> (media-notas (notas aluno)) 9.5)
(list (first aluno) (second aluno))))
turma)))
或者您可以使用循环
:
(defun lista-dos-aprovados (turma)
(loop for aluno in turma
when (> (media-notas (notas aluno)) 9.5)
collect (list (first aluno) (second aluno))))
有什么特别的原因使你不能发布为人类可读性而格式化的代码吗?@RainerJoswig,这是一个反复出现的主题,因此也许指向一篇描述如何这样做的文章会更有建设性。另一个选择是使用mapcan并让lambda函数始终返回列表。它在
(t nil)
案例中已经存在,它只需要另一个子句返回(list(list(first…(second…))
。谢谢!毕竟这么简单!我不知道如何使用When语句