你能在 LISP 中使用 'list' 命令获得点表示吗?
Can you get dot representation using 'list' command in LISP?
我知道通过使用 cons
我们可以得到类似的东西:
> (cons 'b 'c)
(B . C)
发生这种情况是因为单元格被分成两部分并且值为 B
和 C
。
我的问题是,使用 list
可以得到相同的结果吗?
你不能得到 list
到 return 的虚线列表,因为 "the last argument to list
becomes the car
of the last cons
constructed" 在 returned 列表中。
但是,您可以获得 list*
到 return 的点列表。使用list*
函数,最后一个参数变成最后一个cons
的cdr
,所以:
CL-USER> (list* 'a '(b))
(A B)
CL-USER> (list* 'a 'b '())
(A B)
CL-USER> (list* 'a 'b)
(A . B)
CL-USER> (list* 'a '(b c))
(A B C)
CL-USER> (list* 'a 'b '(c))
(A B C)
CL-USER> (list* 'a 'b 'c '())
(A B C)
CL-USER> (list* 'a 'b 'c)
(A B . C)
例如,(list* 'a 'b 'c '())
和 (list 'a 'b 'c)
都等同于:
CL-USER> (cons 'a (cons 'b (cons 'c '())))
(A B C)
但是 (list* 'a 'b 'c)
等同于:
CL-USER> (cons 'a (cons 'b 'c))
(A B . C)
而且,(list* 'a 'b)
等同于:
CL-USER> (cons 'a 'b)
(A . B)
不,你不能,因为列表是最后一个 cdr
为 nil 的缺点单元格列表。 Lisp 打印机知道约定并且在那种情况下不打印点。
;; a cons cell
CL-USER> (cons 'b 'c)
(B . C)
[o|o]--- c
|
b
;; two cons cells, ending with a symbol: a dot.
CL-USER> (cons 'b (cons 'c 'd))
(B C . D)
;; several cons cells, ending with a symbol: still a dot (at the last cons cell):
CL-USER> (cons 'b (cons 'c (cons 'd (cons 'e 'f))))
(B C D E . F)
;; two cons cells, ending with nil: no dot.
CL-USER> (cons 'b (cons 'c nil))
(B C) ;; and not (B C . NIL)
[o|o]---[o|/]
| |
b c
;; with the list constructor:
CL-USER> (list 'b 'c)
(B C)
我知道通过使用 cons
我们可以得到类似的东西:
> (cons 'b 'c)
(B . C)
发生这种情况是因为单元格被分成两部分并且值为 B
和 C
。
我的问题是,使用 list
可以得到相同的结果吗?
你不能得到 list
到 return 的虚线列表,因为 "the last argument to list
becomes the car
of the last cons
constructed" 在 returned 列表中。
但是,您可以获得 list*
到 return 的点列表。使用list*
函数,最后一个参数变成最后一个cons
的cdr
,所以:
CL-USER> (list* 'a '(b))
(A B)
CL-USER> (list* 'a 'b '())
(A B)
CL-USER> (list* 'a 'b)
(A . B)
CL-USER> (list* 'a '(b c))
(A B C)
CL-USER> (list* 'a 'b '(c))
(A B C)
CL-USER> (list* 'a 'b 'c '())
(A B C)
CL-USER> (list* 'a 'b 'c)
(A B . C)
例如,(list* 'a 'b 'c '())
和 (list 'a 'b 'c)
都等同于:
CL-USER> (cons 'a (cons 'b (cons 'c '())))
(A B C)
但是 (list* 'a 'b 'c)
等同于:
CL-USER> (cons 'a (cons 'b 'c))
(A B . C)
而且,(list* 'a 'b)
等同于:
CL-USER> (cons 'a 'b)
(A . B)
不,你不能,因为列表是最后一个 cdr
为 nil 的缺点单元格列表。 Lisp 打印机知道约定并且在那种情况下不打印点。
;; a cons cell
CL-USER> (cons 'b 'c)
(B . C)
[o|o]--- c
|
b
;; two cons cells, ending with a symbol: a dot.
CL-USER> (cons 'b (cons 'c 'd))
(B C . D)
;; several cons cells, ending with a symbol: still a dot (at the last cons cell):
CL-USER> (cons 'b (cons 'c (cons 'd (cons 'e 'f))))
(B C D E . F)
;; two cons cells, ending with nil: no dot.
CL-USER> (cons 'b (cons 'c nil))
(B C) ;; and not (B C . NIL)
[o|o]---[o|/]
| |
b c
;; with the list constructor:
CL-USER> (list 'b 'c)
(B C)