Created
October 8, 2015 15:42
-
-
Save Ummon/9fe1544670ea655542b7 to your computer and use it in GitHub Desktop.
Module 2 - Problem
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 | |
let rec readAge () : int = | |
printf "Age: " | |
match Int32.TryParse(Console.ReadLine()) with | |
| true, age when age > 0 -> age | |
| _ -> | |
printfn "Cannot read the age, please retry.." | |
readAge () | |
let readNextPerson () : (string * int) option = | |
printf "Name (empty to stop inputs): " | |
let name = Console.ReadLine().Trim() | |
if name = "" then None | |
else Some (name, readAge()) | |
let rec readPersons () : (string * int) list = | |
match readNextPerson() with | |
| Some p -> p :: readPersons() | |
| None -> [] | |
let printPerson (name, age) = | |
printfn "name: %s (%s)" name (if age >= 20 then "no longer a teenager" elif age >= 13 then "teenager" else "child") | |
[<EntryPoint>] | |
let main argv = | |
for p in readPersons() do | |
printPerson p | |
Console.ReadKey() |> ignore | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment