我正在寻找一个带有非负整数的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
Run Code Online (Sandbox Code Playgroud)
Ism*_*awi 14
这可能不是那么可读,但......
- List.tabulate(10, fn x => x)
val it = [0,1,2,3,4,5,6,7,8,9] : int list
Run Code Online (Sandbox Code Playgroud)