Advertisement
Guest User

Untitled

a guest
Sep 29th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.62 KB | None | 0 0
  1.  
  2. type Species = string
  3. open System.Web.Caching
  4. open System.Web.Caching
  5. open System.Web.Caching
  6. open System.Windows.Forms
  7. open System.Runtime.CompilerServices
  8. type Location = string
  9. type Time = int
  10.  
  11. type Observation = Species * Location * Time
  12.  
  13. let os:Observation list = [("Owl","L1",3); ("Sparrow","L2",4); ("Eagle","L3",5);
  14.                ("Falcon","L2",7); ("Sparrow","L1",9);  ("Eagle","L1",14)]
  15.  
  16.  
  17. // ========== EXERCISE 2.1 ==========
  18. let obtainLocation (_,l,_) = l:Location
  19.  
  20. let obtainSpecies (s,_,_) = s:Species
  21.  
  22. let rec locationsOf s os =
  23.     match (s, os) with
  24.     | (_,[]) -> []
  25.     | (s, oshead::ostail) when obtainSpecies(oshead) = s -> obtainLocation(oshead) :: locationsOf s ostail
  26.     | (s, oshead::ostail) -> locationsOf s ostail
  27.  
  28. locationsOf "Eagle" os;;
  29. locationsOf "Sparrow" os;;
  30.  
  31.  
  32. // ========== EXERCISE 2.2 ==========
  33. let obtainA (a,_) = a:Species
  34.  
  35. let rec insert a occ =
  36.     match (a, occ) with
  37.     | (a, []) -> [(a,1)]
  38.     | (a, occhead::occtail) when a = obtainA(occhead) -> let (x, y) = occhead
  39.                                                          (x, y + 1)::occtail
  40.     | (a, occhead::occtail)  -> occhead::insert a occtail;;
  41.  
  42. // White-box tests...
  43. // Insertion of observation "Sparrow"
  44. let occ0 = insert "Sparrow" [("Owl", 1); ("Eagle", 2)];;
  45. // Incrementation of obseration "Eagle"
  46. let occ1 = insert "Eagle" occ0;;
  47.  
  48. // Insert observation of "Falcon"
  49. let occ2 = insert "Falcon" occ1;;
  50.  
  51. // Incremention of observation "Sparrow"
  52. let occ3 = insert "Sparrow" occ2;;
  53.  
  54.  
  55. // ========== EXERCISE 2.3 ==========
  56. let rec toCount os =
  57.     match os with
  58.     | [] -> []
  59.     | oshead::ostail -> let (s,l,t) = oshead
  60.                         insert s (toCount ostail);;
  61.  
  62. // ========== EXERCISE 2.4 ==========
  63. let rec select f intv os =
  64.     let (tLow, tHigh) = intv // splits the interval up in lower and upper bound
  65.     match os with
  66.     | [] -> []
  67.     | oshead::ostail -> let (_,_,t) = oshead // obtains t
  68.                         if tLow <= t && t <= tHigh then f(oshead)::select f intv ostail // if t is within interval then call
  69.                         else select f intv ostail;;                                  // f on the current tuple (oshead)
  70.                                                                                   //and concatenate that with the recursive
  71.                                                                                   // call on the rest of os (ostail).
  72.                                                                                   // Else continue on ostail    
  73.  
  74. // ========== EXERCISE 2.5 ==========
  75.  
  76. select (fun (s,l,t) -> (s,l)) (4,9) os;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement