我需要返回一个列表的副本,其中奇数长度列表已加倍(并且仅使用这些预定义函数
List.hd
,List.tl
或List.length
)
oddList [[]; [1];[1;2];[1;2;3];[];[5;4;3;2;1]]
必须返回
oddList [[]; [1; 1]; [1; 2]; [1; 2; 3; 1; 2; 3]; []; [5; 4; 3; 2; 1; 5; 4; 3; 2; 1]]
当我测试我的函数时,它返回:“错误:未绑定值 oddList” 我不知道出了什么问题
let rec oddList l =
if l = [] then []
else
let fst = list.hd l in
let rest = list.tl l in
if length fst mod 2 = 0 then (fist :: (oddList rest))
else ((fst@fst):: (oddList rest));;
OCaml中没有预定义函数
length
,但是有List.length
.
(将此类问题发布到 StackOverflow 时,您应该包含准确的编译器错误消息,并指出它在程序中指向的位置。)