将整数列表拆分为列表元组
Splitting a List of integers into a tuple of lists
试图在 OCaml 中完成此任务浪费了几个小时但无法找出语法错误。
let split l =
let rec split1 (l1, l2) accu =
match (l1, l2) with
| xs, ([] | [_]) -> (accu, xs)
| [], _ -> ([], [])
| x::xs, y::y'::ys -> split1 (xs, ys) x::accu
split1 (l, l) [];;
第 7 行出现语法错误,字符 20-22
但是 F# 中的类似代码可以正常编译和执行:
let split (l :int list) =
let rec split1 (l1 :int list, l2 :int list) (accu :int list) =
match (l1, l2) with
| xs, ([] | [_]) -> (rev accu, xs)
| [], _ -> ([], [])
| x::xs, y::y'::ys -> let t = x::accu in split1 (xs, ys) t
split1 (l, l) [];;
运行 在 F# 中 [1;2;3;4;5;6;7;8;9;1;2;34;5;6] 列表
我得到结果:
([1;2;3;4;5;6;7],[8;9;1;2;34;5;6])
我正在使用基于 Web 的 TryOcaml(4.01.0 版)
在此先感谢您的帮助
let split (l :int list) =
let rec split1 ((l1 :int list), (l2 :int list)) (accu :int list) =
match (l1, l2) with
| xs, ([] | [_]) -> (List.rev accu, xs)
| [], _ -> ([], [])
| x::xs, y::y'::ys -> let t = x::accu in split1 (xs, ys) t
in
split1 (l, l) []
或者
let split l =
let rec split1 (l1, l2 ) accu =
match (l1, l2) with
| xs, ([] | [_]) -> (List.rev accu, xs)
| [], _ -> ([], [])
| x::xs, y::y'::ys -> split1 (xs, ys) (x::accu)
in
split1 (l, l) []
测试:
# split [1;2;3;4;5;6;7;8;9;1;2;34;5;6];;
- : int list * int list = ([1; 2; 3; 4; 5; 6; 7], [8; 9; 1; 2; 34; 5; 6])
试图在 OCaml 中完成此任务浪费了几个小时但无法找出语法错误。
let split l =
let rec split1 (l1, l2) accu =
match (l1, l2) with
| xs, ([] | [_]) -> (accu, xs)
| [], _ -> ([], [])
| x::xs, y::y'::ys -> split1 (xs, ys) x::accu
split1 (l, l) [];;
第 7 行出现语法错误,字符 20-22
但是 F# 中的类似代码可以正常编译和执行:
let split (l :int list) =
let rec split1 (l1 :int list, l2 :int list) (accu :int list) =
match (l1, l2) with
| xs, ([] | [_]) -> (rev accu, xs)
| [], _ -> ([], [])
| x::xs, y::y'::ys -> let t = x::accu in split1 (xs, ys) t
split1 (l, l) [];;
运行 在 F# 中 [1;2;3;4;5;6;7;8;9;1;2;34;5;6] 列表
我得到结果:
([1;2;3;4;5;6;7],[8;9;1;2;34;5;6])
我正在使用基于 Web 的 TryOcaml(4.01.0 版)
在此先感谢您的帮助
let split (l :int list) =
let rec split1 ((l1 :int list), (l2 :int list)) (accu :int list) =
match (l1, l2) with
| xs, ([] | [_]) -> (List.rev accu, xs)
| [], _ -> ([], [])
| x::xs, y::y'::ys -> let t = x::accu in split1 (xs, ys) t
in
split1 (l, l) []
或者
let split l =
let rec split1 (l1, l2 ) accu =
match (l1, l2) with
| xs, ([] | [_]) -> (List.rev accu, xs)
| [], _ -> ([], [])
| x::xs, y::y'::ys -> split1 (xs, ys) (x::accu)
in
split1 (l, l) []
测试:
# split [1;2;3;4;5;6;7;8;9;1;2;34;5;6];;
- : int list * int list = ([1; 2; 3; 4; 5; 6; 7], [8; 9; 1; 2; 34; 5; 6])