- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
(lambda (List arg1 arg2 ... argn))
这样的函数,我可以使用funcall
/apply
使用原始参数调用这些方法,从而修改列表在 lambda 内部。(lambda (arg1 arg2 ... argn &key List))
这样的函数,我只能将 funcall
/apply
与以下副本一起使用这参数,这意味着我无法在函数内修改它们。 使用(lambda (mem arg1 arg2 ... argn))
:
;; Can pass the original lists for modification inside the function:
(funcall #'fn program-memory args-list)
函数可以修改这些列表。
使用 (lambda (arg1 arg2 ... argn &key mem))
,我只能使用原始列表的副本来调用它:
;; can only pass copies of the lists :(
(apply #'fn (concatenate 'list args (list :mem program-memory)))
因此我无法再修改程序内存。
我怎样才能让它发挥作用?即,使用原始列表而不是副本调用该函数。
<小时/>简化旧代码的示例(如 1 中所示):
(defun mem/w (memory address value)
"Writes the value to memory at address. Returns nil."
(setf (elt memory address) value)
nil)
;; sum
(defun sum-op (mem a b o)
(mem/w mem o (+ a b)))
(let ((program (list 1 2 3 4 5 6 7 8))
(args (list 1 2 0)))
(apply #'sum-op
(cons program args))
(print program)) ;; shows modification --> good
完整代码位于 https://github.com/AlbertoEAF/advent_of_code_2019/blob/master/common-lisp/day5.lisp .
最佳答案
对于您调用时发生的情况似乎存在误解:
(concatenate 'list args (list :mem program-memory))
参数列表args
和(list :mem program-memory)
用于构建新列表。在这里你可以使用 append
,像这样:(append args (list :mem program-memory)
。在这两种情况下,原始列表都未修改,但您会得到一个新的参数列表(可能共享最后一个列表,但这是一个细节)。
但是,输入列表和结果列表的内容相同,在串联之前和之后在这些列表中引用完全相同的对象,不存在对象的隐式副本。 p>
让我们看看:
(defclass something () ())
(defvar *something* (make-instance 'something))
当我评估*something*
时,结果对象打印为 #<SOMETHING {10091B1973}>
(打印的表示可能因实现而异;您的对象的身份会有所不同)。
如果我把它放在一个列表中,然后调用copy-list
,结果列表仍然保持相同的值:
(let ((list (list *something*)))
(assert (eq (first list)
(first (copy-list list)))))
如果您将列表存储在列表中,则同样适用,如果没有显式调用复制,则不会递归复制它们。事实上,让我们尝试一下您给出的相同示例,但使用关键字:
;; unrelated, but for this Advent of Code you are going to have
;; performance issues if you treat memory as a list, and not a vector.
;; Here I change it to a vector.
(defun mem/w (memory address value)
"Writes the value to memory at address"
(setf (svref memory address) value))
;; mem is now a keyword argument
(defun sum-op (a b o &key mem)
(mem/w mem o (+ a b)))
(let ((memory (vector 0 2 3 0 0 0 0 0))
(args (list 1 2 0)))
;; using the backquote/slice syntax for brevity
;; but the result is like concatenate/append
(apply #'sum-op `(,@args :mem ,memory))
memory)
最终的内存状态是:
#(3 2 3 0 0 0 0 0)
注意。未定义的行为是改变参数列表本身。
<小时/>编辑:
也许您确实将内存本身与参数连接起来,在这种情况下,在被调用函数中使用了代表内存的新列表,但如果是这样,这是一个错误,因为连接仅应该修改参数列表,不是参数之一。
关于common-lisp - Common Lisp - 如何使用关键字参数调用/应用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60998273/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!