Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- Assigment 4
- Alexander Rosenberg Johansen
- s145706
- *)
- (*
- Exam 2014 may
- Problem 3
- *)
- type Name = string
- type Sex = |M
- |F
- type YearOfBirth = int
- type FamilyTree = P of Name * Sex * YearOfBirth * Children
- and Children = FamilyTree list
- //Test Data
- let fred = P("Fred",M,1970,[])
- let joan = P("Joan",F,1975,[])
- let may = P("May",F,1945,[fred;joan])
- let stanley = P("Stanley",M,1975,[])
- let peter = P("Peter",M,2005,[])
- let bob = P("Bob",M,2008,[])
- let eve = P("Eve",F,2010,[])
- let mary = P("Mary",F,1980,[peter;bob;eve])
- let jane = P("Jane",F,1985,[])
- let joe = P("Joe",M,1950,[stanley;mary;jane])
- let paul = P("Paul",M,1955,[])
- let larry = P("Larry",M,1920,[may;joe;paul])
- (*
- 3.1
- isWF :FamilyTree -> bool
- That check if a family tree is well-formed
- *)
- let rec childVSchild :FamilyTree list -> bool = function
- |[] -> true
- |[X] -> true
- |(P(_,_,yob1,_))::(P(v,vv,yob2,vvv))::clist when yob1<=yob2 -> childVSchild ((P(v,vv,yob2,vvv))::clist)
- |_ -> false
- let parentVSchild (P(n,s,yob,c)) = List.forall(fun (P(a,aa,cyob,aaa)) -> yob<cyob) c
- let rec isWF :FamilyTree -> bool = function
- |P(_,_,_,[]) -> true
- |P(n,s,yob,c) -> (parentVSchild (P(n,s,yob,c))) &&
- (childVSchild c) &&
- (List.forall (fun (P(a,b,c,d)) -> (isWF (P(a,b,c,d))) ) c)
- let test31 = isWF larry
- (*
- 3.2
- makePerson: Name*Sex*YearOfBith -> FamilyTree
- *)
- let makePerson n s yob = P(n,s,yob,[])
- let test32 = makePerson "SashaG" F 1988
- let rec find:FamilyTree*Name -> Sex*YearOfBirth*Name list = function
- |P(n1,s,yob,c),n when n1 = n -> s,yob,(List.foldBack(fun (P(cn,_,_,_)) list -> cn::list) c [])
- |P(_,_,_,c),n -> let a = List.foldBack[] c
- a.Value
- |_ -> failwith "nuub"
- (*
- 3.3
- Delare two mutually reursive funtions:
- insertChildOf: Name -> FamilyTree -> FamilyTree -> FamilyTree option
- insertChildOfInList: Name -> FamilyTree -> Children -> Children option
- *)
- (*
- let insertChildOf n c t =
- match t with
- |P(n,_,yob,)
- *)
- (*
- 3.4
- Delare a funtion find so that find n t extrats information about the person named n in the family tree t.
- This information comprises the sex, year of birth and the names of all children of that person.
- *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement