Skip to content

Instantly share code, notes, and snippets.

@Ummon
Created October 8, 2015 15:42
Show Gist options
  • Save Ummon/9fe1544670ea655542b7 to your computer and use it in GitHub Desktop.
Save Ummon/9fe1544670ea655542b7 to your computer and use it in GitHub Desktop.
Module 2 - Problem
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