-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter_06_ex_5.fs
53 lines (47 loc) · 1.68 KB
/
Chapter_06_ex_5.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Chapter_06_ex_5
#if INTERACTIVE
#r "packages/Unquote.2.2.2/lib/net40/unquote.dll"
#r "packages/NUnit.2.6.2/lib/nunit.framework.dll"
#endif
open System
open System.Globalization
open NUnit.Framework
open Swensen.Unquote
// 6.5 AncestorTree
type AncestorTree =
| Unspecified
| Info of AncestorTree * string * AncestorTree
// Grandfather1 Grandmother1 GrandFather2 unknown/unspec
// father mother
// child
let tree = Info(
Info(
Info(
Unspecified,
"Grandfather1",
Unspecified),
"Father",
Info(
Unspecified,
"Grandmother1",
Unspecified)
),
"Child",
Info(
Info(
Unspecified,
"Grandfather2",
Unspecified),
"Mother",
Unspecified)
)
let maleAncestors tree =
let rec collectMaleAncestors tree isFatherTree collectedValues =
match tree, isFatherTree with
| Unspecified,_ -> collectedValues
| Info(fathers,male,mothers), true -> male::(collectMaleAncestors fathers true collectedValues)
@ (collectMaleAncestors mothers false collectedValues)
| Info(fathers,_,mothers), false -> (collectMaleAncestors fathers true collectedValues)
@ (collectMaleAncestors mothers false collectedValues)
collectMaleAncestors tree false []
test <@ maleAncestors tree = ["Father"; "Grandfather1"] @>