Reputation: 379
I'm trying to make my function return "invalid Input" when it finds an empty sublist.
let func (lst: 'a list list) =
if List.contains [] lst then
printfn "%s" "Invalid Input"
else
List.map List.head lst
This is what I've tried but this of course doesn't work since my if/else returns two different types.
Upvotes: 1
Views: 75
Reputation: 1991
You could use a Result
type:
let func lst =
if List.contains [] lst
then Error "Invalid input"
else Ok (List.map List.head lst)
Upvotes: 2
Reputation: 23521
I think you should raise an exception. By the way, you may want to use List.isEmpty
:
let func (lst: 'a list list) =
if List.contains [] lst then
raise (System.ArgumentException("Invalid Input"))
else
List.map List.head lst
You can also replace raise
with invalidArg "lst"
let func (lst: 'a list list) =
if List.isEmpty lst then invalidArg "lst" "Input is empty"
else List.map List.head lst
Learn more: Exceptions | F# for fun and profit or msdn.
Then you can catch the exception to retrieve the exception.
Upvotes: 1