Advertisement
Guest User

Module 2 Problem

a guest
Oct 7th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.10 KB | None | 0 0
  1. // Learn more about F# at http://fsharp.org
  2. // See the 'F# Tutorial' project for more help.
  3.  
  4. open System
  5.  
  6. type Person = {
  7.     name : String;
  8.     age : int
  9. }
  10.  
  11. let rec inputPerson () =
  12.     printf "Name: "
  13.     let name = Console.ReadLine()
  14.  
  15.     if String.IsNullOrEmpty(name) then
  16.         List.Empty
  17.     else
  18.         printf "Age: "
  19.         let canParse, age = Int32.TryParse(Console.ReadLine())
  20.  
  21.         if canParse then
  22.             let person = {name = name; age = age}
  23.             person :: inputPerson()
  24.         else
  25.             printfn "Could not parse age, ending input phase."
  26.             List.Empty
  27.  
  28. let formatPerson person =
  29.     let statement = match person.age with
  30.     | x when x >= 20 -> "is no longer a teenager."
  31.     | x when x > 13 -> "is a tennager."
  32.     | _ -> "is a kid."
  33.  
  34.     sprintf "%s %s" person.name statement
  35.  
  36. [<EntryPoint>]
  37. let main argv =
  38.     printfn "Input people. Input blank name or invalid age to end input phase."
  39.  
  40.     inputPerson() |> List.iter (fun x -> printfn "%s" (formatPerson x))
  41.  
  42.     Console.ReadKey() |> ignore
  43.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement