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.180 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.179 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.178 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.177 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.176 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.175 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.174 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.173 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.172 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.171 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.170 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.169 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.167 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.166 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.165 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.164 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.163 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.162 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.161 tracks/fsharp/exercises/robot-simulator/Example.fs
trackler-2.2.1.160 tracks/fsharp/exercises/robot-simulator/Example.fs