【发布时间】:2016-11-16 16:14:04
【问题描述】:
我想创建一个产生两个输出的函数。 请考虑以下示例:
我构建了两个函数,给定一个整数列表,返回偶数位置的元素和奇数位置的元素列表。
let rec alternate1 lst =
match lst with
[] -> []
| [x] -> []
| x::y::xs -> y::(alternate1 xs)
let rec alternate2 lst =
match lst with
[] -> []
| [x] -> [x]
| x::y::xs -> x::(alternate2 xs)
这里一切都很好。现在,问题来了:我想创建一个 single 函数alternate,它返回带有签名alternate: int list-> (int list * int list) 的两个列表。
let rec alternate lst =
match lst with
[] -> []
| [x] -> []
| [x::y] -> [y]
(*My attempts:*)
| x::y::xs -> ((y::alternate xs), (x::alternate xs))
| x::y::xs -> [(y::alternate xs); (x::alternate xs)]
| x::y::xs -> ((y::alternate xs) && (x::alternate xs))
到目前为止,没有任何解决方案奏效。我很确定这个问题甚至很愚蠢,但我的reference 并没有帮助我解决问题。
【问题讨论】:
-
为什么不返回两个列表的元组?
-
那会很理想,但我仍然无法将其付诸实践。