0 people like it.
Like the snippet!
Risk Assignment in F#
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
|
// Type defs and helper methods - basic oo stuffs
type State =
| State of string * State seq
and User =
| User of string * int
let getContigs state =
match state with
| State(_,contigs) -> contigs
let getName state =
match state with
| State(name, _) -> name
let getUserName user=
match user with
| User(name, _) -> name
let stateTaken state takenStates =
let matches s =
match s with
| (s, u) when s = state -> true
| _ -> false
Seq.exists matches takenStates
let printUser user =
match user with
| User(name, weight) -> printfn "User: %s %d" name weight
printfn "Initializing"
// Set up the data
let rec mo = State("mo", seq { yield il; yield ia;yield ne; yield ks; yield ok; yield ar; yield tn;yield ky })
and ne = State("ne", seq {yield ia; yield ks; yield mo})
and ok = State("ok", seq {yield ks;yield ar;yield mo})
and ar = State("ar", seq {yield ok; yield mo; yield tn})
and tn = State("tn", seq {yield ar; yield mo;yield ky})
and ky = State("ky", seq { yield tn; yield il; yield mo})
and ks = State("ks", seq {yield ne; yield ok; yield mo})
and il = State("il", seq { yield mo; yield ia; yield ky })
and ia = State("ia", seq { yield mo; yield il; yield ne;yield ks })
let allStates = [mo; ne;ok;ar;tn;ky;ks;il; ia]
let users = [User("josh", 2); User("nick" , 3); User("mark", 2)]
// Implementation
let rec placeNextUser (state:State) (users:User list) takenStates =
match users with
| [] -> printStates takenStates
| user :: remainingusers -> placeUserOnState user state remainingusers takenStates
and placeUserOnState user state remainingusers takenStates =
let takenStates = (state, user) :: takenStates
match user with
| User(_, weight) when weight = 1 -> nextOpenState state remainingusers takenStates
| User(name, weight) -> nextOpenState state (User(name, weight-1) :: remainingusers) takenStates
and nextOpenState state (users:User list ) takenStates =
let contigs = getContigs state
let state = Seq.tryFind (fun state-> (stateTaken state takenStates) = false) contigs
match state with
| Some(state) -> placeNextUser state users takenStates
| None -> let state = Seq.find (fun state -> (stateTaken state takenStates) = false) allStates
placeNextUser state users takenStates
and printStates takenStates : unit =
match takenStates with
| (state, user)::rest -> printfn "%s was taken by %s" (getName state) (getUserName user)
printStates rest
| [] -> ()
placeNextUser mo users []
|
Multiple items
union case State.State: string * seq<State> -> State
--------------------
type State = | State of string * seq<State>
Full name: Script.State
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
type User = | User of string * int
Full name: Script.User
Multiple items
union case User.User: string * int -> User
--------------------
type User = | User of string * int
Full name: Script.User
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
val getContigs : state:State -> seq<State>
Full name: Script.getContigs
val state : State
val contigs : seq<State>
val getName : state:State -> string
Full name: Script.getName
val name : string
val getUserName : user:User -> string
Full name: Script.getUserName
val user : User
val stateTaken : state:'a -> takenStates:seq<'a * 'b> -> bool (requires equality)
Full name: Script.stateTaken
val state : 'a (requires equality)
val takenStates : seq<'a * 'b> (requires equality)
val matches : ('a * 'c -> bool) (requires equality)
val s : 'a * 'c (requires equality)
val s : 'a (requires equality)
val u : 'c
module Seq
from Microsoft.FSharp.Collections
val exists : predicate:('T -> bool) -> source:seq<'T> -> bool
Full name: Microsoft.FSharp.Collections.Seq.exists
val printUser : user:User -> unit
Full name: Script.printUser
val weight : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val mo : State
Full name: Script.mo
val ne : State
Full name: Script.ne
val ok : State
Full name: Script.ok
val ar : State
Full name: Script.ar
val tn : State
Full name: Script.tn
val ky : State
Full name: Script.ky
val ks : State
Full name: Script.ks
val il : State
Full name: Script.il
val ia : State
Full name: Script.ia
val allStates : State list
Full name: Script.allStates
val users : User list
Full name: Script.users
val placeNextUser : state:State -> users:User list -> takenStates:(State * User) list -> unit
Full name: Script.placeNextUser
val users : User list
type 'T list = List<'T>
Full name: Microsoft.FSharp.Collections.list<_>
val takenStates : (State * User) list
val printStates : takenStates:(State * User) list -> unit
Full name: Script.printStates
val remainingusers : User list
val placeUserOnState : user:User -> state:State -> remainingusers:User list -> takenStates:(State * User) list -> unit
Full name: Script.placeUserOnState
val nextOpenState : state:State -> users:User list -> takenStates:(State * User) list -> unit
Full name: Script.nextOpenState
val state : State option
val tryFind : predicate:('T -> bool) -> source:seq<'T> -> 'T option
Full name: Microsoft.FSharp.Collections.Seq.tryFind
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val find : predicate:('T -> bool) -> source:seq<'T> -> 'T
Full name: Microsoft.FSharp.Collections.Seq.find
type unit = Unit
Full name: Microsoft.FSharp.Core.unit
val rest : (State * User) list
More information