如何在 Common Lisp 中递增或递减一个数字?
Common Lisp 递增/递减数字和/或数值变量的惯用方法是什么?
What is the idiomatic Common Lisp way to increment/decrement numbers and/or numeric variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想使用结果而不修改原始数字(参数),请使用内置的“+”或“-”函数或其简写“1+”或“1-”。如果您确实想修改原始位置(包含数字),请使用内置的“incf”或“decf”函数。
使用加法运算符:
或者,如果您愿意,可以使用以下简写:
请注意,Common Lisp 规范将上述两种形式定义为在含义上等效,并建议实现使它们在性能上等效。虽然这是一个建议,但根据 Lisp 专家的说法,任何“自重”的实现都应该不会出现性能差异。
如果你想更新 num (不仅仅是获取 1 + 它的值),那么使用“incf”:
注意:
你还可以使用 incf/decf 来增加(减少)超过 1 个单位:
有关更多信息,请参阅 Common Lisp超规格:
1+
incf/decf
Use the built-in "+" or "-" functions, or their shorthand "1+" or "1-", if you just want to use the result, without modifying the original number (the argument). If you do want to modify the original place (containing a number), then use the built-in "incf" or "decf" functions.
Using the addition operator:
Or, if you prefer, you could use the following short-hand:
Note that the Common Lisp specification defines the above two forms to be equivalent in meaning, and suggests that implementations make them equivalent in performance. While this is a suggestion, according to Lisp experts, any "self-respecting" implementation should see no performance difference.
If you wanted to update num (not just get 1 + its value), then use "incf":
NOTE:
You can also use incf/decf to increment (decrement) by more than 1 unit:
For more information, see the Common Lisp Hyperspec:
1+
incf/decf