Common Lisp中的Python range()模拟 [英] Python's range() analog in Common Lisp

查看:95
本文介绍了Common Lisp中的Python range()模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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.

相关: Scheme中Python范围的模拟

推荐答案

没有生成数字序列的内置方法,这样做的规范方法是执行以下操作之一:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆