Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.00 KB | None | 0 0
  1. type Name = string;;
  2. type Sex =  | M // male
  3.             | F // female
  4. type YearOfBirth = int;;
  5. type FamilyTree = P of Name * Sex * YearOfBirth * Children
  6. and Children = FamilyTree list
  7.  
  8. let fred = P("Fred",M,1970,[])
  9. let joan = P("Joan",F,1975,[])
  10. let may = P("May",F,1945,[fred;joan])
  11.  
  12.  
  13. let stanley = P("Stanley",M,1975,[])
  14.  
  15. let peter = P("Peter",M,2005,[])
  16. let bob = P("Bob",M,2008,[])
  17. let eve = P("Eve",F,2010,[])
  18. let mary = P("Mary",F,1980,[peter;bob;eve])
  19. let jane = P("Jane",F,1985,[])
  20. let joe = P("Joe",M,1950,[stanley;mary;jane])
  21.  
  22. let paul = P("Paul",M,1955,[])
  23. let larry = P("Larry",M,1920,[may;joe;paul])
  24.  
  25.  
  26. let findN p tree =
  27.         let rec findInner t =
  28.             match t with
  29.             | P(n,_,_,_) when p(n) -> Some(t)
  30.             | P(_,_,_,children) -> children |>  List.choose (findInner)
  31.                                             |>  List.tryFind(fun _ -> true)
  32.             | P(_,_,_,[]) -> None
  33.         findInner tree
  34.  
  35. let test34 = findN (fun n -> n = "May") larry
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement