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.68 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.67 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.66 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.65 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.64 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.63 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.62 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.61 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.60 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.59 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.58 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.57 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.56 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.55 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.54 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.53 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.52 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.51 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.50 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.49 tracks/fsharp/exercises/robot-simulator/Example.fs