Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let (|EmptySet|_|) a = if Set.isEmpty a then Some () else None
- let (|SetHead|_|) set =
- match set |> Set.toList with
- | head::_ -> Some head
- | _ -> None
- let hasZeroSum set =
- let rec hasZeroSum sum set =
- match set with
- | EmptySet -> false // Empty set doesn't count
- | SetHead head when (head + sum) = 0 -> // Is first element + current sum = 0? Success!
- true
- | _ ->
- // Try any (element + current sum) with rest of set
- set |> Set.exists (fun i ->
- set
- |> Set.remove i
- |> hasZeroSum (i + sum)
- )
- hasZeroSum 0 set
- [<EntryPoint>]
- let main argv =
- [10; 20; -5; -13; -3; 1; 56]
- |> Set.ofList
- |> hasZeroSum
- |> printfn "Has any zero sums? %A"
- 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement