Created
January 10, 2014 15:36
-
-
Save TheRealCodeBeard/8356493 to your computer and use it in GitHub Desktop.
FSharp Lesson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
(*Version 1 | |
let rec handle (command:string) = | |
match command.ToLower() with | |
| "quit" -> printfn "Bye Bye!"//exit condition first! | |
| _ -> | |
printfn "I don't understand %s" command | |
Console.ReadLine() |> handle | |
Console.ReadLine() |> handle | |
*) | |
(*Version 2 | |
let quitMessage() = printfn "Bye Bye!" | |
let processCommand command = printfn "Can't do %s" command | |
let rec handle (command:string) = | |
match command.ToLower() with | |
| "quit" -> quitMessage() | |
| _ -> | |
processCommand command | |
Console.ReadLine() |> handle | |
Console.ReadLine() |> handle | |
*) | |
(*Version 3 | |
let processCommand command = printfn "Can't do %s" command | |
let rec handle (command:string) = | |
let quit() = printfn "Bye Bye!" | |
let evaluate() = | |
processCommand command | |
Console.ReadLine() |> handle | |
match command.ToLower() with | |
| "quit" -> quit() | |
| _ -> evaluate() | |
Console.ReadLine() |> handle | |
*) | |
(*Version 4 | |
type Result = | |
| Quit | |
| Continue | |
let isQuit (command:string) = command.ToLower() = "quit" | |
let execute command = printfn "Can't do %s" command | |
let evaluate command = | |
if command |> isQuit then Quit | |
else execute command; Continue | |
let rec handle (command:string) = | |
match evaluate command with | |
| Quit -> printfn "Bye Bye!" | |
| Continue -> Console.ReadLine() |> handle | |
Console.ReadLine() |> handle | |
*) | |
(*Version 5 | |
type Result = | |
| Quit | |
| Continue | |
let isQuit (command:string) = command.ToLower() = "quit" | |
let execute command = printfn "Can't do %s" command | |
let evaluate command = | |
if command |> isQuit then Quit | |
else execute command; Continue | |
let next fn = | |
Console.Write("> ") | |
Console.ReadLine() |> fn | |
let rec handle (command:string) = | |
match evaluate command with | |
| Quit -> printfn "Bye Bye!" | |
| Continue -> handle |> next | |
handle |> next | |
*) | |
(*Version 6*) | |
type Result = | |
| Quit | |
| Continue | |
type ColouredConsole(colour) = | |
do Console.ForegroundColor <- colour | |
member this.Write message = Console.Write(message:string) | |
interface IDisposable with | |
member this.Dispose() = Console.ResetColor() | |
let execute command = printfn "Can't do %s" command | |
let isQuit (command:string) = command.ToLower() = "quit" | |
let evaluate command = | |
if command |> isQuit then Quit | |
else execute command; Continue | |
let writeIn colour message = | |
use cc = new ColouredConsole(colour) | |
cc.Write message | |
let next fn = | |
writeIn ConsoleColor.DarkGray "> " | |
Console.ReadLine() |> fn | |
let rec handle (command:string) = | |
match evaluate command with | |
| Quit -> printfn "Bye Bye!" | |
| Continue -> handle |> next | |
writeIn ConsoleColor.Cyan "Welcome! (type 'quit' to exit)\n" | |
handle |> next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment