Discussion:
[Lisp-cn] 关于广义变量(Generalized Variables)方面的疑问
Vito Van
2016-08-10 10:07:17 UTC
Permalink
最近圚读《On Lisp》看到第12章第4小节时有䞀些䞍明癜之倄圚歀向各䜍请教

章节圚线阅读
http://www.ituring.com.cn/article/53771
Ctrl+F12.4 曎倍杂的实甚工具

这䞀郚分诎道芁讟计䞀䞪宏 _f 可以理解䞺

(_f + x y) 等同于 (incf x y)

然后给出了䞀䞪错误的䟋子

(defmacro _f (op place &rest args) ; wrong
'(setf ,place (,op ,place ,@args)))

错误的原因是“䞍幞的是我们无法甚 define-modify-macro 正确无误地定义 _f 因䞺应甚到广义变量䞊的操䜜笊是由参数给定的。”我没有理解䞺什么“应甚到广义变量䞊的操䜜笊是由参数给定的”就䞍胜正确无误的定义 _f 了呢

我试了试这䞪错误的䟋子华也没发现明星的错误还请各䜍垮忙解答䞀䞋感激䞍尜

以䞋䞺错误版 _f 的展匀

CL-USER> (macroexpand '(_f * x 10))
(SETQ X (* X 10))
T
CL-USER> (macroexpand-1 '(_f * (get 'prop x) 10))
(SETF (GET 'PROP X) (* (GET 'PROP X) 10))
T
CL-USER> (macroexpand-1 '(_f * (incf (aref a (incf i))) 10))
(SETF (INCF (AREF A (INCF I))) (* (INCF (AREF A (INCF I))) 10))
T
--
--
Lisp-cn(Lisp䞭文甚户组)
CLUG http://lisp.org.cn

---
悚收到歀邮件是因䞺悚订阅了 Google 眑䞊论坛的“Lisp-cn(Lisp䞭文甚户组)”矀组。
芁退订歀矀组并停止接收歀矀组的电子邮件请发送电子邮件到lisp-cn+***@googlegroups.com。
芁查看曎倚选项请访问 https://groups.google.com/d/optout。
Vito Van
2016-08-10 11:19:00 UTC
Permalink
等䞍急了于是去 IRC #lisp 问了䞀䞋貌䌌是解决了聊倩记圕圚䞋面。

然后我觉埗“䞍幞的是我们无法甚 define-modify-macro 正确无误地定义 _f 因䞺应甚到广义变量䞊的操䜜笊是由参数给定的。” 这句话跟错误的䟋子没有关系只䞍过是圚诎无法甚 define-modify-macro 定义眢了。

<VitoVan> Hey guys, good day.
<VitoVan> I recently is reading <On Lisp>, I am stuck in chapter 12.4, http://ep.yimg.com/ty/cdn/paulgraham/onlisp.pdf
<VitoVan> page 184
<VitoVan> Which the author is trying to construct a macro _f to make things like this: (_f + x y) the same as (incf x y)
<VitoVan> and then have given a wrong example: (defmacro _f (op place &rest args) '(setf ,place (,op ,place ,@args)))
<VitoVan> which I can't find anything wrong with this example, and I can't understand why "Unfortunately, we can’t define a correct f with define-modify-macro, because the operator to be applied to the generalized variable is given as an argument."
<VitoVan> Hope anyone can help, thank you very much!
* wccoder has quit (Ping timeout: 264 seconds)
<VitoVan> Forgive my poor English, I hope that won't bothering you.
<malice`> VitoVan: setf is a quasi-magical macro that replaces "setf X Y" with proper form
<Grue``> fwiw I tried (let ((x (list 1))) (_f + (car x) 10) x) and it works
<Grue``> there might be some complex case where it fails though
<Grue``> something about ,place being evaluated twice
<VitoVan> malice`: then what's the problem?
<Grue``> for example Graham offers the example (incf (aref a (incf i))). with the naive implementation i would be incremented twice
<Grue``> or something
<VitoVan> Grue``: Yes, I aware the eval twice thing, but it should be *not good* instead of **wrong**
<VitoVan> Thank you Grue``
<jdz> in C that would be "not good", in CL it is "wrong"
<VitoVan> Right, the double evals of incf is truly wrong.
* quasus has quit (Ping timeout: 276 seconds)
* shdeng (~***@220.249.11.58<mailto:***@220.249.11.58>) has joined
* impaktor has quit (Read error: Connection reset by peer)
<VitoVan> Thank you very much Grue``, and malice and jdz, problem solved, what a magical place~

On Wed, Aug 10, 2016 at 6:08 PM Vito Van <***@live.com<mailto:***@live.com>> wrote:
最近圚读《On Lisp》看到第12章第4小节时有䞀些䞍明癜之倄圚歀向各䜍请教

章节圚线阅读
http://www.ituring.com.cn/article/53771
Ctrl+F12.4 曎倍杂的实甚工具

这䞀郚分诎道芁讟计䞀䞪宏 _f 可以理解䞺

(_f + x y) 等同于 (incf x y)

然后给出了䞀䞪错误的䟋子

(defmacro _f (op place &rest args) ; wrong
'(setf ,place (,op ,place ,@args)))

错误的原因是“䞍幞的是我们无法甚 define-modify-macro 正确无误地定义 _f 因䞺应甚到广义变量䞊的操䜜笊是由参数给定的。”我没有理解䞺什么“应甚到广义变量䞊的操䜜笊是由参数给定的”就䞍胜正确无误的定义 _f 了呢

我试了试这䞪错误的䟋子华也没发现明星的错误还请各䜍垮忙解答䞀䞋感激䞍尜

以䞋䞺错误版 _f 的展匀

CL-USER> (macroexpand '(_f * x 10))
(SETQ X (* X 10))
T
CL-USER> (macroexpand-1 '(_f * (get 'prop x) 10))
(SETF (GET 'PROP X) (* (GET 'PROP X) 10))
T
CL-USER> (macroexpand-1 '(_f * (incf (aref a (incf i))) 10))
(SETF (INCF (AREF A (INCF I))) (* (INCF (AREF A (INCF I))) 10))
T
--
--
Lisp-cn(Lisp䞭文甚户组)
CLUG http://lisp.org.cn

---
悚收到歀邮件是因䞺悚订阅了Google眑䞊论坛䞊的“Lisp-cn(Lisp䞭文甚户组)”矀组。
芁退订歀矀组并停止接收歀矀组的电子邮件请发送电子邮件到lisp-cn+***@googlegroups.com<mailto:lisp-cn+***@googlegroups.com>。
芁查看曎倚选项请访问https://groups.google.com/d/optout。
--
--
Lisp-cn(Lisp䞭文甚户组)
CLUG http://lisp.org.cn

---
悚收到歀邮件是因䞺悚订阅了 Google 眑䞊论坛的“Lisp-cn(Lisp䞭文甚户组)”矀组。
芁退订歀矀组并停止接收歀矀组的电子邮件请发送电子邮件到lisp-cn+***@googlegroups.com。
芁查看曎倚选项请访问 https://groups.google.com/d/optout。
继续阅读narkive:
Loading...