Sha256: 2540059502ff2bae7d21e348b595f80161848871bfe3ff8300eba6a96a79690b

Contents?: true

Size: 1.28 KB

Versions: 306

Compression:

Stored size: 1.28 KB

Contents

module RobotSimulator

type Bearing = North | East | South | West
type Coordinate = int * int

type Robot = { bearing: Bearing; coordinate: Coordinate }

let createRobot bearing coordinate = { bearing = bearing; coordinate = coordinate }

let turnLeft robot = 
    match robot.bearing with
    | North -> { robot with bearing = West  }
    | East  -> { robot with bearing = North }
    | South -> { robot with bearing = East  }
    | West  -> { robot with bearing = South }

let turnRight robot = 
    match robot.bearing with
    | North -> { robot with bearing = East  }
    | East  -> { robot with bearing = South }
    | South -> { robot with bearing = West  }
    | West  -> { robot with bearing = North }

let advance robot = 
    let (x, y) = robot.coordinate

    match robot.bearing with
    | North -> { robot with coordinate = (x    , y + 1) }
    | East  -> { robot with coordinate = (x + 1, y    ) }
    | South -> { robot with coordinate = (x    , y - 1) }
    | West  -> { robot with coordinate = (x - 1, y    ) }
    
let applyInstruction robot instruction =
    match instruction with
    | 'L' -> turnLeft robot
    | 'R' -> turnRight robot
    | 'A' -> advance robot
    | _   -> failwith "Invalid instruction"

let simulate robot instructions = Seq.fold applyInstruction robot instructions

Version data entries

306 entries across 306 versions & 1 rubygems

Version Path
trackler-2.2.1.88 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.87 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.86 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.85 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.84 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.83 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.82 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.81 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.80 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.79 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.78 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.77 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.76 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.75 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.74 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.73 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.72 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.71 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.70 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.69 tracks/fsharp/exercises/robot-simulator/Example.fs