Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In this problem we are going to create a simple console application that reads in the name and age of a person.
- //We need to input multiple peoples details which we specify at the start of the application.
- //We then output each persons name and age as well as a field which is determined by the following rules:
- //
- // If age >= 20 then the field is a string which states the person's name and that they are no longer a teenager
- // 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
- // If the age is < than 13 then is states the name and the person is a kid or child.
- //
- //As part of this exercise your application must handle erroneous input.
- //
- //To complete this assignment please complete the Q and A and peer review.
- open System
- let (|Float|_|) str =
- match System.Double.TryParse(str) with
- | (true, float) -> Some(float)
- | _ -> None
- let (|String|_|) str =
- if String.IsNullOrWhiteSpace(str) then
- None
- else
- Some(str)
- let readWithMessage (msg:string) =
- Console.Write(msg)
- Console.ReadLine()
- let rec readFloatWithMessage msg =
- match readWithMessage msg with
- | Float f -> f
- | _ -> readFloatWithMessage msg
- let rec readStringWithMessage msg =
- match readWithMessage msg with
- | String s -> s
- | _ -> readStringWithMessage msg
- [<EntryPoint>]
- let main argv =
- let name = readStringWithMessage "What is your name: "
- let age = readFloatWithMessage "What is your age: "
- match age with
- | x when x >= 20.0 -> printfn "L O, %s! You are no longer a teenager." name
- | x when x < 20.0 && x >= 13.0 -> printfn "L O, %s! You are a teenager." name
- | x when x < 13.0 -> printfn "L O, %s! You are a kid or child." name
- | _ -> printfn "Something went wrong!"
- Console.ReadKey()|>ignore
- 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement