我没有在Common-Lisp中得到这第一/最后一件事。是的,我知道它是如何工作的,但是我不明白为什么它会那样工作。

基本上,要获得列表中的第一项,我可以使用(first mylist)。但是,如果我想要最后一个项目,(last mylist)不会给我那个;相反,它给了我一个列表,其中包含列表中的最后一项!

(我正在使用Clozure-CL,它对我来说似乎还有一些其他问题,但是,由于我是Lisp-n00b,因此我尽量避免掉旧的“解释器坏了!”的把戏。 :))

因此,例如:

? (setq x '((1 2) (a b)))
=> ((1 2) (A B))

? (first x)
=> (1 2)  ; as expected

? (last x)
=> ((A B))  ; why a list with my answer in it?!

? (first (last x))
=> '(A B)  ; This is the answer I'd expect from plain-old (last x)

有人可以帮我理解为什么会这样吗?我使用这些物品的方式有误吗? first是真的很奇怪吗?!

谢谢!

最佳答案

在Common Lisp中,last应该从documentation返回一个列表:

last list &optional n => tail
list---a list, which might be a dotted list but must not be a circular list.
n---a non-negative integer. The default is 1.
tail---an object.



例如:
(setq x (list 'a 'b 'c 'd))
(last x) =>  (d)

是的,这是违反直觉的。在Lisp的其他版本中,它按名称所建议的那样起作用,例如在Racket(一种方言)中:
(define x '((1 2) (a b)))
(first x) => '(1 2)
(last x) => '(a b)

(define x (list 'a 'b 'c 'd))
(last x) =>  'd

关于lisp - 常见Lisp : first returns first,,但是last返回一个last的列表-是吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17731670/

10-11 06:33