Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.90 KB | None | 0 0
  1. //In this problem we are going to create a simple console application that reads in the name and age of a person.
  2. //We need to input multiple peoples details which we specify at the start of the application.
  3. //We then output each persons name and age as well as a field which is determined by the following rules:
  4. //
  5. //    If age >= 20 then the field is a string which states the person's name and that they are no longer a teenager
  6. //    If the age < 20 and greater than 13 then the field is a string that states the persons name and that they are a teenage
  7. //    If the age is < than 13 then is states the name and the person is a kid or child.
  8. //
  9. //As part of this exercise your application must handle erroneous input.
  10. //
  11. //To complete this assignment please complete the Q and A and peer review.
  12.  
  13. open System
  14.  
  15. let (|Float|_|) str =
  16.     match System.Double.TryParse(str) with
  17.     | (true, float) -> Some(float)
  18.     | _ -> None
  19.  
  20. let (|String|_|) str =
  21.     if String.IsNullOrWhiteSpace(str) then
  22.         None
  23.     else
  24.         Some(str)
  25.  
  26. let readWithMessage (msg:string) =
  27.     Console.Write(msg)
  28.     Console.ReadLine()
  29.  
  30. let rec readFloatWithMessage msg =
  31.     match readWithMessage msg with
  32.     | Float f -> f
  33.     | _ -> readFloatWithMessage msg
  34.  
  35. let rec readStringWithMessage msg =
  36.     match readWithMessage msg with
  37.     | String s -> s
  38.     | _ -> readStringWithMessage msg
  39.  
  40.  
  41. [<EntryPoint>]
  42. let main argv =
  43.     let name = readStringWithMessage "What is your name: "
  44.     let age = readFloatWithMessage "What is your age: "
  45.     match age with
  46.     | x when x >= 20.0 -> printfn "L O, %s! You are no longer a teenager." name
  47.     | x when x < 20.0 && x >= 13.0 -> printfn "L O, %s! You are a teenager." name
  48.     | x when x < 13.0 -> printfn "L O, %s! You are a kid or child." name
  49.     | _ -> printfn "Something went wrong!"
  50.     Console.ReadKey()|>ignore
  51.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement