use "std/env.nib" use "std/io.nib" use "std/iter.nib" data Taken where Taken(Int) interface Seats where free(seat: Int) -> Bool; hold(seat: Int, who: String) -> Int effector Hall is Seats where var owner :: Int => String = {1: "", 2: "", 3: "", 4: ""}; free(seat) = owner[seat] == "" hold(seat, who) = owner[seat] := who; seat module Box where func price :: seat: Int -> Int = if seat <= 2 then 4800 else 3200 func house :: seats: Vec -> Vec = Vec.map(seats, { s -> if s <= 2000 then 4800 else 3200 }) func takings :: prices: Vec -> Int = Vec.fold(prices, 0, { a, p -> a + p }) func season :: -> Int = takings(house(Iter.to_vec(Iter.range(1, 20001)))) func choose :: seat: Int -> Int ! Seats raises Taken = if @Seats.free(seat) then seat else raise Taken(seat) func sell :: seat: Int, who: String -> Int ! Seats, Io.Print raises Taken = let s = @Seats.hold(choose(seat), who); let _ = @Io.Print.println("{who}: seat {s}, {price(s)} cents"); price(s) func main :: -> Int ! Seats, Env.Vars, Io.Print = let who = try @Env.Vars.get("WHO") rescue Err.Missing(_) => "nobody" end; try sell(2, who) + sell(2, "bo") rescue Taken(s) => resume(s + 1) end func rush :: -> Int ! Seats = let first = @Seats.hold(2, "ada"); @Seats.hold(2, "bo") wire Box runs Box.main where Seats = Hall, Env.Vars = Env.Process, Io.Print = Io.Stdout wire Rush runs Box.rush where Seats = Hall wire Season runs Box.season