它只返回一些“”,但我需要返回“none”。
我该怎么做?
let cat(filenames: string list) : string option =
try
let b = filenames |> List.map (fun x -> (defaultArg (concat(x)) ""))
Some (String.concat "" b)
with _ -> None
它只返回一些“”,但我需要返回“none”。
我该怎么做?
let cat(filenames: string list) : string option =
try
let b = filenames |> List.map (fun x -> (defaultArg (concat(x)) ""))
Some (String.concat "" b)
with _ -> None
some ""
,因为空列表不会引发异常。您需要匹配空列表并返回None。我也不确定List.map
是否与您在此处尝试进行的连接相匹配,也许您想使用List.reduce
?像这样做可能会有用。let cat filenames =
match filenames with
| [] -> None
| l -> l |> List.reduce (+) |> Some