编辑代码

open System
type Car_positions = int list
type Cars = {time: int; positions: Car_positions}

let move_cars positions = positions |> List.map ( fun x -> if float(Random().Next(0, 10)) / 10. > 0.3 then x + 1 else x )

let output_car pos = String.replicate pos ">"

let run_step_of_race state = {time= state.time - 1; positions= move_cars state.positions}

let draw state = state.positions |> List.map output_car |> fun ls -> List.zip [1 .. List.length ls] ls |> List.iter ( fun x -> let n, a = x in printfn "%i%s" n a ) ; printfn "%s" ""

let rec race state = if state.time > 0 then let next = run_step_of_race state in ( draw next; race next )

race {time= 10; positions= [0; 0; 0; 0; 0]}