【发布时间】:2013-11-24 02:44:14
【问题描述】:
我需要一个可以接受任意数量参数的函数,每个参数可以是'T 或seq<'T> 类型。在函数内部,我需要将其作为单个 seq<'T> 进行处理,所有输入的组合顺序与它们提供的顺序相同。
显而易见的方法是:
module Test =
let flatten ([<ParamArray>] args) =
let flat = seq {
for a in args do
match box a with
| :? int as x -> yield x
| :? seq<int> as sq ->
for s in sq do
yield s
| _ -> failwith "wrong input type"
}
flat // this should be seq<int>
但即使是最简单的情况,我也无法让它在 FSI 中工作
let fl = Test.flatten 1;;
----------------------^
...: error FS0001: The type 'int' is not compatible with the type 'seq<'a>'
这里出了什么问题以及如何根据需要使其工作?或许这可以通过一些完全不同的方式来完成?
【问题讨论】:
标签: f# f#-interactive