有没有一种方法可以调整常见的lisp点宏,以免从零开始,而是从其他数字(例如1)开始?
我正在使用Emacs,Slime和SBCL。
dotimes
的默认使用是:
CL-USER> (defun my-dotimes (n)
(dotimes (i n)
(format t "~s ~%" i)))
生成:
CL-USER> (my-dotimes 10)
0
1
2
3
4
5
6
7
8
9
NIL
我希望该函数可以从一个开始计数。我可以通过它来更改:
CL-USER> (defun my-new-dotimes (n)
(dotimes (i (- n 1))
(format t "~s ~%" (+ i 1))))
MY-NEW-DOTIMES
CL-USER> (my-new-dotimes 10)
1
2
3
4
5
6
7
8
9
NIL
但是,它并不像一个优雅的解决方案。
官方文档提到neclare
可能性。但是我不确定如何使用它。
有更好的方法吗?
I am using Emacs, Slime, and SBCL.
The default use of dotimes
is:
CL-USER> (defun my-dotimes (n)
(dotimes (i n)
(format t "~s ~%" i)))
Which generates:
CL-USER> (my-dotimes 10)
0
1
2
3
4
5
6
7
8
9
NIL
I wish the function could start counting from one. I can change it with:
CL-USER> (defun my-new-dotimes (n)
(dotimes (i (- n 1))
(format t "~s ~%" (+ i 1))))
MY-NEW-DOTIMES
CL-USER> (my-new-dotimes 10)
1
2
3
4
5
6
7
8
9
NIL
But, it does not feel like an elegant solution.
The official documentation mentions a declare
possibility. But I am not sure how to use it.
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,没有办法使用
dotimes
来做到这一点。如果您需要一个从1
计算的宏,则使用loop
或do
或编写一个。使用
做
:No, there is no way to do that with
dotimes
. If you want a macro which counts from1
, either useloop
ordo
or write one.Using
do
: