Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.28 KB | None | 0 0
  1. (*
  2. Assigment 4
  3. Alexander Rosenberg Johansen
  4. s145706
  5. *)
  6.  
  7. (*
  8. Exam 2014 may
  9. Problem 3
  10. *)
  11.  
  12. type Name = string
  13. type Sex =  |M
  14.             |F
  15. type YearOfBirth = int
  16.  
  17. type FamilyTree = P of Name * Sex * YearOfBirth * Children
  18. and Children = FamilyTree list
  19. //Test Data
  20.  
  21.  
  22. let fred = P("Fred",M,1970,[])
  23. let joan = P("Joan",F,1975,[])
  24. let may = P("May",F,1945,[fred;joan])
  25.  
  26.  
  27. let stanley = P("Stanley",M,1975,[])
  28.  
  29. let peter = P("Peter",M,2005,[])
  30. let bob = P("Bob",M,2008,[])
  31. let eve = P("Eve",F,2010,[])
  32. let mary = P("Mary",F,1980,[peter;bob;eve])
  33. let jane = P("Jane",F,1985,[])
  34. let joe = P("Joe",M,1950,[stanley;mary;jane])
  35.  
  36. let paul = P("Paul",M,1955,[])
  37. let larry = P("Larry",M,1920,[may;joe;paul])
  38. (*
  39. 3.1
  40. isWF :FamilyTree -> bool
  41. That check if a family tree is well-formed
  42. *)
  43. let rec childVSchild :FamilyTree list -> bool = function
  44.     |[] -> true
  45.     |[X] -> true
  46.     |(P(_,_,yob1,_))::(P(v,vv,yob2,vvv))::clist when yob1<=yob2 -> childVSchild ((P(v,vv,yob2,vvv))::clist)
  47.     |_ -> false
  48. let parentVSchild (P(n,s,yob,c)) = List.forall(fun (P(a,aa,cyob,aaa)) -> yob<cyob) c
  49.  
  50. let rec isWF :FamilyTree -> bool = function
  51.     |P(_,_,_,[]) -> true
  52.     |P(n,s,yob,c) -> (parentVSchild (P(n,s,yob,c))) &&
  53.                             (childVSchild c) &&
  54.                                      (List.forall (fun (P(a,b,c,d)) -> (isWF (P(a,b,c,d))) ) c)
  55. let test31 = isWF larry
  56.  
  57. (*
  58. 3.2
  59. makePerson: Name*Sex*YearOfBith -> FamilyTree
  60. *)
  61.  
  62. let makePerson n s yob = P(n,s,yob,[])
  63. let test32 = makePerson "SashaG" F 1988
  64.  
  65. let rec find:FamilyTree*Name -> Sex*YearOfBirth*Name list = function
  66.     |P(n1,s,yob,c),n when n1 = n -> s,yob,(List.foldBack(fun (P(cn,_,_,_)) list -> cn::list) c [])
  67.     |P(_,_,_,c),n -> let a = List.foldBack[] c
  68.                      a.Value
  69.     |_ -> failwith "nuub"
  70.  
  71.  
  72. (*
  73. 3.3
  74. Delare two mutually reursive funtions:
  75. insertChildOf: Name -> FamilyTree -> FamilyTree -> FamilyTree option
  76. insertChildOfInList: Name -> FamilyTree -> Children -> Children option
  77. *)
  78. (*
  79. let insertChildOf n c t =
  80.     match t with
  81.     |P(n,_,yob,)
  82.  
  83. *)
  84.  
  85.  
  86.  
  87. (*
  88. 3.4
  89. Delare a funtion find so that find n t extrats information about the person named n in the family tree t.
  90. This information comprises the sex, year of birth and the names of all children of that person.
  91.  
  92. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement