LISP很简单的清单问题 [英] LISP very simple list question

查看:104
本文介绍了LISP很简单的清单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习口齿不清,我对此很陌生,所以我想知道...

Im learning lisp and im pretty new at this so i was wondering...

如果我这样做:

(defparameter *list-1* (list 1 2))
(defparameter *list-2* (list 2 3))
(defparameter *list-3* (append *list-1* *list-2*))

然后

(setf (first *list-2*) 1)
*list-3*

我会得到(1 2 1 4)

I will get (1 2 1 4)

我知道这是因为追加将保存资源"并为第一个块创建一个新列表,但实际上只会指向第二个块,如果我这样做的话,因为:

I know this is because the append is going to "save resources" and create a new list for the first chunk, but will actually just point to the second chunk, coz if i do:

(setf (first *list-1*) 0)
*list-3*

我将获得(1 2 1 4)更逻辑(0 2 1 4)的安装

I will get (1 2 1 4) instade of the more logical (0 2 1 4)

所以我的问题是,在Lisp中还有什么其他情况,您怎么知道这些不直观或不一致的东西呢?

So my question is, what other cases are like this in lisp, how do you black belt lispers know how to deal with this stuff that is not intuitive or consistent?

推荐答案

一种防御策略是避免共享结构.

One defensive tactic is to avoid sharing structure.

(defparameter *list-3* (append *list-1* *list-2* '()))

(defparameter *list-3* (append *list-1* (copy-list *list-2*)))

现在,新的*list-3*的结构是全新的,对*list-3*的修改不会影响*list-2*,反之亦然.

Now the structure of the new *list-3* is all new, and modifications to *list-3* won't affect *list-2* and vice versa.

这篇关于LISP很简单的清单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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