Created
October 13, 2015 09:37
-
-
Save craigrbruce/722de77f85d249c82915 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
let getUserInput message predicate = | |
let mutable loop = true | |
let mutable userInput = ""; | |
while loop do | |
printf "%s" message; | |
userInput <- Console.ReadLine(); | |
let isValid = predicate(userInput) | |
if isValid = true then loop <- false | |
else | |
Console.WriteLine("Input was not in the correct format") | |
userInput | |
let isNumber str = | |
let valid, number = Int32.TryParse str | |
valid | |
let isString str = | |
let result = String.IsNullOrWhiteSpace str | |
not result | |
let displayResult name age = | |
if age >= 20 then | |
printfn "%s is no longer a kid" name | |
elif age > 13 && age < 20 then | |
printfn "%s is a teenager" name | |
else | |
printfn "%s is a kid" name | |
[<EntryPoint>] | |
let main argv = | |
let name = getUserInput "Please enter the person's name: " isString | |
let ageStr = getUserInput "Please enter the person's age: " isNumber | |
let age = Int32.Parse ageStr | |
displayResult name age | |
0 // return an integer exit code | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment