Common Lisp中的Python range()模拟 [英] Python's range() analog in Common Lisp
问题描述
如何在Common Lisp中创建连续数字的列表?
换句话说,Python的<$ c等效于什么Common Lisp中的$ c> range 函数?
In other words, what is the equivalent of Python's range
function in Common Lisp?
在Python中 range(2,10,2)
返回 [2、4、6、8]
,第一个和最后一个参数是可选的。尽管Emacs Lisp具有数字序列
,但我找不到创建数字序列的惯用方式。
In Python range(2, 10, 2)
returns [2, 4, 6, 8]
, with first and last arguments being optional. I couldn't find the idiomatic way to create a sequence of numbers, though Emacs Lisp has number-sequence
.
可以使用循环宏使用循环宏来模拟范围,但是我想知道一种可接受的方式来生成以start和终点和步骤。
Range could be emulated using loop macro, but i want to know the accepted way to generate a sequence of numbers with start and end points and step.
推荐答案
没有生成数字序列的内置方法,这样做的规范方法是执行以下操作之一:
There is no built-in way of generating a sequence of numbers, the canonical way of doing so is to do one of:
- 使用
循环
- 编写一个使用
loop
- Use
loop
- Write a utility function that uses
loop
一个示例实现为(仅接受从低到高的计数):
An example implementation would be (this only accepts counting "from low" to "high"):
(defun range (max &key (min 0) (step 1))
(loop for n from min below max by step
collect n))
这允许您指定(可选)最小值和
This allows you to specify an (optional) minimum value and an (optional) step value.
要生成奇数:(范围10:min 1:step 2)
这篇关于Common Lisp中的Python range()模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!