Skip to content

Instantly share code, notes, and snippets.

@danielmarbach
Created May 12, 2020 20:16
Show Gist options
  • Save danielmarbach/f900c9e8a5f991b8a64e38463cd99051 to your computer and use it in GitHub Desktop.
Save danielmarbach/f900c9e8a5f991b8a64e38463cd99051 to your computer and use it in GitHub Desktop.
Some fiddling around with FSharp
open System
let petrolOfCar = 100
type Destination = Home | Office | Stadium | GasStation
let driveTo destination currentPetrol =
let remainingPetrol =
match destination with
| Home -> currentPetrol - 25
| Office -> currentPetrol - 50
| Stadium -> currentPetrol - 25
| GasStation -> (currentPetrol - 10) + 50
match remainingPetrol with
| belowZero when belowZero < 0 -> failwith "Not enough petrol"
| _ -> remainingPetrol
let lowerCase (x: string) = x.ToLower()
let rec getDestination() =
let input = Console.ReadLine() |> lowerCase
match input with
| "home" -> Destination.Home
| "office" -> Destination.Office
| "stadium" -> Destination.Stadium
| "gas station" -> Destination.GasStation
| _ -> getDestination()
let main petrolOfCar =
let mainInner petrol =
try
let destination = getDestination()
printfn "Trying to drive to %A" destination
let petrolLeft = driveTo destination petrol
printfn "Made it to %A! You have %i petrol left" destination petrolLeft
petrolLeft
with ex ->
printfn "ERROR: %s" ex.Message
petrol
petrolOfCar |> Seq.unfold (fun petrolLeft -> Some(petrolLeft, petrolLeft |> mainInner))
for x in main petrolOfCar do ignore x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment