SHARE
TWEET

Untitled

a guest Mar 21st, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. open System
  2.  
  3. // ========== Type declaration ==========
  4. type Drink =
  5.     | Cappuccino // 30 DKK
  6.     | Espresso   // 25 DKK
  7.     | Latte      // 20 DKK
  8.  
  9.     | Coke       // 15 DKK
  10.     | Sprite     // 10 DKK
  11.     | Fanta      // 5  DKK
  12.  
  13. type Size =
  14.     | Small      // 0  DKK
  15.     | Medium     // 5  DKK
  16.     | Large      // 10 DKK
  17.  
  18. type Container =
  19.     | Cup  
  20.     | Can
  21.     | Bottle
  22.  
  23. type Order = { drink: Drink; size: Size; container: Container }
  24.  
  25. // ========== Function for calculating price ==========
  26. let calculatePrice (p : Order) =
  27.     match p with
  28.         | { drink = Cappuccino; size = Small } -> 30 + 0
  29.         | { drink = Cappuccino; size = Medium } -> 30 + 5
  30.         | { drink = Cappuccino; size = Large } -> 30 + 10
  31.  
  32.         | { drink = Espresso; size = Small } -> 25 + 0
  33.         | { drink = Espresso; size = Medium } -> 25 + 5
  34.         | { drink = Espresso; size = Large } -> 25 + 10
  35.  
  36.         | { drink = Latte; size = Small } -> 20 + 0
  37.         | { drink = Latte; size = Medium } -> 20 + 5
  38.         | { drink = Latte; size = Large } -> 20 + 10
  39.  
  40.         | { drink = Coke; size = Small } -> 15 + 0
  41.         | { drink = Coke; size = Medium } -> 15 + 5
  42.         | { drink = Coke; size = Large } -> 15 + 10
  43.  
  44.         | { drink = Sprite; size = Small } -> 10 + 0
  45.         | { drink = Sprite; size = Medium } -> 10 + 5
  46.         | { drink = Sprite; size = Large } -> 10 + 10
  47.  
  48.         | { drink = Fanta; size = Small } -> 5 + 0
  49.         | { drink = Fanta; size = Medium } -> 5 + 5
  50.         | { drink = Fanta; size = Large } -> 5 + 10
  51.  
  52. // ========== Test ==========
  53. let order1 = {drink = Coke; size = Medium; container = Bottle}
  54. let order2 = {drink = Fanta; size = Medium; container = Can}
  55. let order3 = {drink = Latte; size = Small; container = Cup}
  56.  
  57. let orders = [ order1; order2; order3 ]
  58.  
  59. let resultList =
  60.     [
  61.     for i in 0 .. orders.Length - 1 do
  62.         yield calculatePrice orders.[i]
  63.     ]
  64.  
  65. let resultSum = List.sum resultList
  66.  
  67. printfn "Total price: %d" resultSum
  68.  
  69. [<EntryPoint>]
  70. let main argv =
  71.     printfn "Hello World from F#!"
  72.     0 // return an integer exit code
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top