Sha256: b807ff3ebed4e4ad9f7444136c06d0e99d9bbaeae9fd46062fc65a4ba853b908

Contents?: true

Size: 1.29 KB

Versions: 90

Compression:

Stored size: 1.29 KB

Contents

module RobotSimulator

type Direction = North | East | South | West
type Position = int * int

type Robot = { direction: Direction; position: Position }

let create bearing position = { direction = bearing; position = position }

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

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

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

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

let instructions instructions' robot = Seq.fold applyInstruction robot instructions'

Version data entries

90 entries across 90 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.97 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.96 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.95 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.94 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.93 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.92 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.91 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.90 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.89 tracks/fsharp/exercises/robot-simulator/Example.fs