如何用nil创建点对

时间:2016-11-13 10:35:01

标签: list linked-list lisp common-lisp cons

我有以下表格的队列名称列表:

'("foo" "bar")

我试图以下列方式将队列存储为关联列表:

'(("foo" . nil) ("bar" . nil))

基本上它是一个包含队列“foo”和“bar”的关联列表,它们当前是空的。当Bob和Alice将在“foo”队列中时,它应如下所示。

'(("foo" . ("alice" "bob")) ("bar" . nil))

如何创建此结构?我试着通过写:

来实现这一目标
(mapcar #'(lambda (x) (cons x ''nil)) '("foo" "bar"))

返回

'(("foo" QUOTE NIL) ("bar" QUOTE NIL))

这可能不是我想要的,因为当我试图将Bob推到“foo”队列时,它根本无法正常工作。

* (setf *tmp* '(("foo" . 'nil) ("bar" . 'nil)))
(("foo" QUOTE NIL) ("bar" QUOTE NIL))
* (push "bob" (caddr (assoc "foo" *tmp* :test #'string=)))
* *tmp*
(("foo" QUOTE ("bob")) ("bar" QUOTE NIL))

如何在点后创建带空列表的点对?

修改 实际上,当我将这样的关联列表存储为类槽时,它看起来很好。

* (describe myClassInstance)
;; ...
QUEUES    = (("foo" . 'NIL) ("bar" . 'NIL))
;; ...

然而,在将Bob添加到“foo”队列之后,所有队列都在被更改。

* (push "bob" (caddr (assoc "foo" (slot-value myClassInstance 'testClass::queues) :test #'string=))))
* (describe myClassInstance)
;; ...
QUEUES    = (("foo" . '("bob") ("bar" . '("bob"))
;; ...

这里发生了什么?看起来所有队列的cdr部分都是单个符号,当我在一个地方(“foo”队列)更改它的值时,它在所有地方(所有队列)都被更改了。它有意义吗?

3 个答案:

答案 0 :(得分:5)

我认为你可能会将结构与其印刷表示混淆。 (cons x nil)'(x . nil)相同,与'(x)相同。它们都将打印为(x)

如果你想打印为'(x . nil),你可以为它写一个打印功能,但表示非常好。

答案 1 :(得分:4)

cdr为nil的缺点与单元素列表完全相同。这是列表的定义方式。

换句话说,(cons x nil)(list x)相同。你可以想象这样的结果:

+-------+
| x |nil|
+-------+

cdr是列表的虚线对也只是一个列表。

换句话说,'(("foo" . nil) ("bar" . nil))'(("foo") ("bar"))完全相同,即使前一种符号可能更好地传达了将其视为一个列表的意图。

同样,'(("foo" . ("alice" "bob")) ("bar" . nil))'(("foo" "alice" "bob") ("bar"))完全相同。

这意味着您可以完全按照自己的意愿创建数据结构,但可以使用e。 G。 list代替(lambda (x) (cons x nil))(对于单个参数,它是相同的)。

(defun make-queues (&rest names)
  (mapcar #'list names))

您也可以按名称推送assoc找到的元素:

(defun add-to-queue (queues queue-name item)
  (push item (cdr (assoc queue-name queues :test #'equal))))

(defun get-queue (queues queue-name)
  (cdr (assoc queue-name queues :test #'equal)))

你最后一个问题是你把一个文字放到一个列表中并尝试修改它:你把只包含nil文字列表放到你的列表的每个元素中

答案 2 :(得分:2)

与点数比较

ratioU <- function(nvals)
{
  h_x = function(x) exp(-x)
  # u- is b-, u+ is b+ and v+ is a in the example:
  uminus = 0
  uplus = 2/exp(1)
  vplus = 1
  X.vals <- NULL
  i <- 0
  repeat {
    i <- i+1
    u <- runif(1,0,vplus)
    v <- runif(1,uminus,uplus)
    X <- u/v
    if(v^2 <= h_x(X)) {
      tmp <- X
    }
    else {
      next
    }
    X.vals <- c(X.vals,tmp)
    if(length(X.vals) >= nvals) break
  }
  answer <- X.vals  
  answer
}

sol = ratioU(1000) 
par(mfrow=c(1,2))
hist(sol,breaks=50, main= "using ratioU",freq=F)
hist(rexp(1000),breaks = 50, main="using rexp from R",freq=F)
par(mfrow=c(1,1))

par(mfrow=c(1,2))
plot(density(sol))
plot(density(rexp(1000)))
par(mfrow=c(1,1))

且没有点

let strokeTextAttributes = [
        NSStrokeColorAttributeName : UIColor.black,
        NSForegroundColorAttributeName : UIColor.white,
        NSStrokeWidthAttributeName : -4.0,
        NSFontAttributeName : UIFont.boldSystemFont(ofSize: 30)
    ]

    self.attributedText = NSMutableAttributedString(string: self.text!, attributes: strokeTextAttributes)

因此,两种符号都描述了相同的缺点结构。