【发布时间】:2013-05-04 07:58:46
【问题描述】:
我正在寻找一个 SML 函数,它接受一个非负整数并返回从 0 到但不包括给定值的所有整数的列表,类似于 Python 中的 range()。是的,我可以(并且已经)编写了我自己的,但我更喜欢内置的东西,我不需要复制和粘贴到我想使用它的每个项目中。有什么想法吗?提前致谢!
% Python code
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(* SML code: my implementation; I'd prefer a built-in version *)
fun range x =
let fun helper current stop =
if current = stop
then nil
else current :: (helper (current + 1) stop)
in helper 0 x
end;
(* my code when run *)
- range 10;
val it = [0,1,2,3,4,5,6,7,8,9] : int list
【问题讨论】: