clojure:没有坏细胞 [英] clojure: no cons cells

查看:122
本文介绍了clojure:没有坏细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说,与大多数lisp语言一样,clojure没有 cons细胞 .

i heard that clojure does not have cons cells as of most lisp languages.

这是否意味着clojure列表不会以空列表结尾?

does that mean a clojure list does not end with an empty list?

有人能解释一下这到底是什么意思吗?

could anyone explain what that exactly means?

推荐答案

Lisp提供了原始的cons数据结构及其表示法.

请参见 John McCarthy,符号表达式的递归函数及其由机器进行的计算,第一部分,1960年,第3章,符号表达式的递归函数.

该章介绍:

  • 由原子组成的符号表达式和使用点表示法编写的成对符号表达:( a . b )
  • 缩写某些符号表达式的列表符号(a b c)
  • 用于终止列表的原子符号nil
  • 原始函数carcdrconseqatom
  • 其他几个功能:ffsubstequalnullcadrcaddrnullappendamongpairassocsublisapplyeval,...
  • Symbolic expressions made of atoms and pairs of symbolic expressions written using a dot notation: ( a . b )
  • a list notation to abbreviate certain symbolic expressions (a b c)
  • an atomic symbol nil to terminate lists
  • the primitive functions car, cdr, cons, eq and atom
  • several other functions: ff, subst, equal, null, cadr, caddr, null, append, among, pair, assoc, sublis, apply, eval, ...

早在Lisp中,就添加了使cons细胞突变的功能:rplaca(表示替换汽车)和rplacd(表示替换cdr ).请参阅John McCarthy等人的 LISP 1.5程序员手册al.从1962年起.这些函数使我们能够编写破坏性函数,也使我们能够创建基于循环cons的数据结构,例如循环列表.

Early on in Lisp, functions to mutate cons cells have been added: rplaca (means replace car) and rplacd (means replace cdr). See the LISP 1.5 Programmer's Manual by John McCarthy et al. from 1962. These functions allow us to write destructive functions and also allow us to create cyclic cons-based data structures like cyclic lists.

常见的Lisp

典型地,Lisp方言实现了大部分功能. Common Lisp也不例外,Common Lisp标准对此功能进行了描述: Conses .使用上述功能的示例:

Typically Lisp dialects implement most of this. Common Lisp is no exception and for it this functionality is described in the Common Lisp standard: Conses. Examples using the functions mentioned above:

; pair two lists into a list of cons cells.
; the function pair is called pairlis in Common Lisp.
CL-USER 17 > (pairlis '(john mary eva) '(34 29 40))
((EVA . 40) (MARY . 29) (JOHN . 34))

; find a cons cell in a list of cons cells,
; based on the content of the car of those cons cells
CL-USER 18 > (assoc 'eva (pairlis '(john mary eva)
                                  '(34 29 40)))
(EVA . 40)

; create a tree out of cons cells and atoms
CL-USER 19 > (cons (cons 10 20) (cons 30 40))
((10 . 20) 30 . 40)

; a cons cell is not an atom
CL-USER 20 > (atom (cons 1 2))
NIL

; a cons cell is not nil
CL-USER 21 > (null (cons 1 2))
NIL

; substitute an item with a new one in a tree
CL-USER 22 > (subst 30                          ; new
                    'bar                        ; old
                    '((10 . 20) . (bar . 40)))  ; tree
((10 . 20) 30 . 40)   ; also written as  ((10 . 20) . (30 . 40))

; substitute several items in a tree, using an assoc list
; to describe the substitutions
CL-USER 23 > (sublis '((a . 10) (d . 40))      ; substitutions
                     '((a . b) . (c . d)))     ; tree
((10 . B) C . 40)

那么列表就是符号表达式的一种特殊情况.它们通常以不带点的形式书写:

Lists are then a special case of symbolic expressions. They are typically written without dots:

CL-USER 24 > '(a . (b . nil))
(A B)

公用Lisp还支持Lisp 1.5的变异操作rplacarplacd:

Common Lisp also supports the mutating operations rplaca and rplacd of Lisp 1.5:

CL-USER 25 > (let ((c (cons 0 1)))              ; create a cons
               (print c)                        ; print it
               (print (rplaca c 'foo))          ; replace the car
               (print (rplacd c 'bar))          ; replace the cdr
               (print (eq c (rplaca c 'baz)))   ; identical ?
               (values))
(0 . 1)      ; the cons cell
(FOO . 1)    ; car replaced
(FOO . BAR)  ; cdr replaced
T            ; still the same object

Emacs Lisp

Emacs Lisp还实现了上述功能:

Emacs Lisp also implements the above functionality:

ELISP> (sublis '((a . 10) (d . 40))                                             
               '((a . b) . (c . d)))
((10 . b) c . 40)

服装

Clojure 不支持这些符号表达式.它没有缺点单元格,没有点表示法,并且不提供上述接口.例如, atom 在Clojure中的含义完全不同. cons不会创建一个cons单元.列表不是由cons单元组成的.

Clojure does not support these symbolic expressions as described by John McCarthy. It has no cons cells, no dot notation and does not provide the above interface. For example atom means something completely different in Clojure. cons does not create a cons cell. Lists are not made of cons cells.

在Clojure中,圆点只是另一个符号:

In Clojure a dot is just another symbol:

user=> (count '(1 . 2))
3

有一个原始函数可以构造 lists :

There is a primitive function to construct lists:

user=> (list 1 2 3)
(1 2 3)

结果应为列表:

user=> (list? (list 1 2 3))
true

有一个名为cons的函数:

user=> (cons 0 (list 1 2 3))
(0 1 2 3)

这不是列表:

user=> (list? (cons 0 (list 1 2 3)))
false

基本上Clojure确实使用不同的数据结构(-> 序列逻辑列表 )及其自己的命名和语义.即使名称与Lisp名称相似,也不要指望它们具有相同的功能.

Basically Clojure does use different data structures (-> sequences, logical lists) with its own naming and semantics. Even if names are similar to Lisp names, don't expect that they do the same.

方案

编程语言 Scheme 还提供了与上面类似的con单元.它缺少某些功能,但可以轻松实现.例如,sublis可以在Scheme中这样实现(请参见 initdr. scm ):

The programming language Scheme also provides cons cells similar to above. It lacks some of the functions, but they can be easily implemented. For example sublis might be implemented like this in Scheme (see initdr.scm):

(define (sublis alist tree)
  (if (pair? tree)
      (cons (sublis alist (car tree))
            (sublis alist (cdr tree)))
      (if (assv tree alist)
          (cdr (assv tree alist))
          tree)))

这篇关于clojure:没有坏细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆