YoungT
YoungT

Reputation: 45

How to return "None" in Fsharp?

It just returns some " ", but I need to return "none".

How can I do this ?

let cat(filenames: string list) : string option =
    try
        let b = filenames |> List.map (fun x -> (defaultArg (concat(x)) ""))
        Some (String.concat "" b)
    with  _ -> None

Upvotes: 3

Views: 686

Answers (1)

VoronoiPotato
VoronoiPotato

Reputation: 3173

It returns some "" because an empty list doesn't cause an exception. You need to match on an empty list and return None. I also am not sure the List.map aligns with the concat you're trying to do here, perhaps you meant List.reduce ? Something like this might work.

let cat filenames = 
   match filenames with
   | [] -> None
   | l -> l |> List.reduce (+) |> Some

Upvotes: 4

Related Questions