Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Species = string
- open System.Web.Caching
- open System.Web.Caching
- open System.Web.Caching
- open System.Windows.Forms
- open System.Runtime.CompilerServices
- type Location = string
- type Time = int
- type Observation = Species * Location * Time
- let os:Observation list = [("Owl","L1",3); ("Sparrow","L2",4); ("Eagle","L3",5);
- ("Falcon","L2",7); ("Sparrow","L1",9); ("Eagle","L1",14)]
- // ========== EXERCISE 2.1 ==========
- let obtainLocation (_,l,_) = l:Location
- let obtainSpecies (s,_,_) = s:Species
- let rec locationsOf s os =
- match (s, os) with
- | (_,[]) -> []
- | (s, oshead::ostail) when obtainSpecies(oshead) = s -> obtainLocation(oshead) :: locationsOf s ostail
- | (s, oshead::ostail) -> locationsOf s ostail
- locationsOf "Eagle" os;;
- locationsOf "Sparrow" os;;
- // ========== EXERCISE 2.2 ==========
- let obtainA (a,_) = a:Species
- let rec insert a occ =
- match (a, occ) with
- | (a, []) -> [(a,1)]
- | (a, occhead::occtail) when a = obtainA(occhead) -> let (x, y) = occhead
- (x, y + 1)::occtail
- | (a, occhead::occtail) -> occhead::insert a occtail;;
- // White-box tests...
- // Insertion of observation "Sparrow"
- let occ0 = insert "Sparrow" [("Owl", 1); ("Eagle", 2)];;
- // Incrementation of obseration "Eagle"
- let occ1 = insert "Eagle" occ0;;
- // Insert observation of "Falcon"
- let occ2 = insert "Falcon" occ1;;
- // Incremention of observation "Sparrow"
- let occ3 = insert "Sparrow" occ2;;
- // ========== EXERCISE 2.3 ==========
- let rec toCount os =
- match os with
- | [] -> []
- | oshead::ostail -> let (s,l,t) = oshead
- insert s (toCount ostail);;
- // ========== EXERCISE 2.4 ==========
- let rec select f intv os =
- let (tLow, tHigh) = intv // splits the interval up in lower and upper bound
- match os with
- | [] -> []
- | oshead::ostail -> let (_,_,t) = oshead // obtains t
- if tLow <= t && t <= tHigh then f(oshead)::select f intv ostail // if t is within interval then call
- else select f intv ostail;; // f on the current tuple (oshead)
- //and concatenate that with the recursive
- // call on the rest of os (ostail).
- // Else continue on ostail
- // ========== EXERCISE 2.5 ==========
- select (fun (s,l,t) -> (s,l)) (4,9) os;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement