在ocaml中建立一个整数列表

| 我想编写一个在两个整数(包括两个整数)之间建立列表的函数 rec myFunc x y将建立一个列表,其中包含x和y之间的所有整数,包括x和y 对于现在的逻辑,我有这样的事情:
let rec buildList i n = let x = i+1 in if i <= n then i::(buildList x n)
但这给了我一个错误“表达式的类型为列表”,但是期望表达式的类型为unit。 我以为buildList返回一个int列表,而我作为int,所以cons运算符将是有效的,但是它说应该是无效的? 为什么会发生这种情况,我该如何解决?     
已邀请:
如果条件为真,则返回列表
i::(buildList x n)
。如果不正确,您还会返回什么? 不满足条件时,在函数中添加“ 2”以返回空列表。 当您没有任何“ 3”时,编译器会认为它是“ 4”(因此出现错误信息)。     
您的
if
缺少
else
条件 我建议您使用尾部递归函数:
let buildList x y =
  let (x,y) = if x<y then (x,y) else (y,x) in
  let rec aux cpt acc =
      if cpt < x then acc
      else aux (cpt-1) (cpt::acc)
  in aux y []
首先,请确保您正确地确定了边界的顺序(防白痴),然后通过使用累加器的局部递归函数构造列表。     
依靠电池包装的两种选择, 使用展开,目的是建立清单,
let range ~from:f ~until:u = 
    BatList.unfold f (function | n when n <= u -> Some (n, succ n) | _ -> None)
使用Enum,允许使用惰性数据结构,
# BatList.of_enum @@ BatEnum.(1--9);;
- : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9]
    
我的建议是,这尊重论证的次序。
let rec iota n m = 
  let oper = if n < m then succ else pred  in 
    if n = m then [n] else n :: iota (oper n) m
编辑: 运算符的选择位于递归部分内,最好像这样在外部:
let iota n m = 
  let oper = if n < m then succ else pred  in 
    let rec f1 n m = if n = m then [n] else n :: f1 (oper n) m in
      f1 n m
在超过200000个元素时,我得到了堆栈溢出(所以我们到了)
# iota 0 250000;;
Stack overflow during evaluation (looping recursion?).
待办事项:尾递归     
let buildList i n =
 let rec aux acc i =
   if i <= n then
     aux (i::acc) (i+1)
   else (List.rev acc)
 in
 aux [] i
测试:
# buildList 1 3;;
- : int list = [1; 2; 3]
# buildList 2 1;;
- : int list = []
# buildList 0 250000;;
- : int list =
[0; 1; 2; 3; .... 296; 297; 298; ...]
    

要回复问题请先登录注册