Advertisement
Guest User

L5

a guest
Oct 31st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.62 KB | None | 0 0
  1. module FizzBuzz =
  2.     let matchFizzBuzz i =
  3.         match (i, i%3=0, i%5=0) with
  4.         | (i, true, true) -> sprintf "%A FizzBuzz" i
  5.         | (i, true, false) -> sprintf "%A Fizz" i
  6.         | (i, false, true) -> sprintf "%A Buzz" i
  7.         | (i, false, false) -> sprintf "%A" i
  8.  
  9.     let fizzBuzz =
  10.         List.iter(fun i -> printfn "%A" (matchFizzBuzz i)) [1..20]
  11.    
  12. module Bisects =
  13.     let isBisect year =
  14.         match year with
  15.         | year when year % 400 = 0 -> Some(year)
  16.         | year when year % 100 = 0 -> None
  17.         | year when year % 4 = 0 -> Some(year)
  18.         | _ -> None
  19.  
  20.     let chooseLeapYears =
  21.             [ 1900; 2000; 2016; 2017 ]
  22.             |> List.choose isBisect
  23.             |> List.iter (printfn "%d")
  24.    
  25. module Timey =
  26.     let duration list =
  27.         match list with
  28.         | [] -> 0
  29.         | [ s ] -> s
  30.         | [ m; s ] -> m * 60 + s
  31.         | h :: m :: s :: _ -> h * 3600 + m * 60 + s
  32.        
  33. module Shapes =
  34.     open System
  35.     type Shape =
  36.         | Rectangle of height : float * width : float
  37.         | Circle of radius : float
  38.  
  39.     let area shape =  
  40.         match shape with
  41.         | Rectangle(h, w) -> h*w
  42.         | Circle(r) -> Math.PI * r * r
  43.      
  44.     let rec readValidFloat () =
  45.         let input = Console.ReadLine()
  46.         let ok, num = Double.TryParse input
  47.        
  48.         match ok, num with
  49.         | true, num when num > 0.0 -> num
  50.         | _, _ -> readValidFloat()
  51.  
  52.     let rec validChoice() =
  53.         printf "Shape type: "
  54.         let shapeType = Console.ReadLine()
  55.         match shapeType with
  56.         | "C" | "c" -> 'C'
  57.         | "D" | "d" -> 'D'
  58.         | _ -> validChoice()
  59.  
  60.     let inputRectangle() =
  61.         printfn "Introduceti lungimea:"
  62.            
  63.         let h = readValidFloat()
  64.         printfn "Introduceti latimea:"
  65.         let w = readValidFloat()
  66.         Rectangle(h,w)
  67.    
  68.     let inputCircle() =
  69.         printfn "Introduceti raza:"
  70.         Circle(readValidFloat())
  71.    
  72.     let inputShape() =
  73.         printfn "Introduceti D pentru dreptunghi si C pentru cerc: "
  74.         if validChoice() = 'D' then inputRectangle()
  75.         else inputCircle()
  76.  
  77. [<EntryPoint>]
  78. let main argv =
  79.     FizzBuzz.fizzBuzz
  80.     Bisects.chooseLeapYears
  81.     printfn "--------------------------------------"
  82.     printfn "%d" (Timey.duration [23; 9; 5; 1; 4])
  83.     printfn "%d" (Timey.duration [])
  84.     printfn "%d" (Timey.duration [3])
  85.     printfn "%d" (Timey.duration [2; 30])
  86.  
  87.     let s = Shapes.inputShape()
  88.     printfn "Aria figurii '%A' este: %.2f" s (Shapes.area s)
  89.  
  90.  
  91.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement