Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.09 KB | None | 0 0
  1. // Learn more about F# at http://fsharp.org
  2.  
  3. open System
  4.  
  5. type sandwichRec = | Ham | Salami | Cheese
  6. type saladRec = | Caesar | Waldorf | Mixed
  7. type cakeRec = | Carrot | Vanilla | Chocolate
  8.  
  9. type Food = | Sandwich of sandwichRec | Salad of saladRec | Cake of cakeRec
  10.  
  11. type Size = | Small | Medium | Large
  12.  
  13. type ShoppingBag = {food: Food; size: Size}
  14.  
  15. let aUser: ShoppingBag = {food = Sandwich (sandwichRec.Ham); size = Size.Large}
  16.  
  17. let getPriceForBag shoppingBag =
  18.     let priceForFood =
  19.         match shoppingBag.food with
  20.         | Sandwich (sandRec) ->
  21.             match sandRec with
  22.             | Ham -> 10
  23.             | Salami -> 15
  24.             | Cheese -> 25
  25.         | Salad (saldRec) ->
  26.             match saldRec with
  27.             | Caesar -> 10
  28.             | Waldorf -> 15
  29.             | Mixed -> 25
  30.         | Cake (cakeRec) ->
  31.             match cakeRec with
  32.             | Carrot -> 5
  33.             | Vanilla -> 15
  34.             | Chocolate -> 30
  35.     let sizePrice =
  36.         match shoppingBag.size with
  37.         | Small -> 5
  38.         | Medium -> 10
  39.         | Large -> 25
  40.  
  41.     (sizePrice + priceForFood)
  42.  
  43. type CanteenMessage =
  44.     |OrderFood of ShoppingBag * int
  45.     |LeaveAComment of string
  46.  
  47. let userExample: CanteenMessage = OrderFood(aUser, 4)
  48. let canteenFoodAgent =
  49.     MailboxProcessor.Start (fun inbox ->
  50.         let rec processFood state =
  51.             async {
  52.                 let! msg = inbox.Receive ()
  53.                 match msg with
  54.                 | OrderFood (bag, qty)->
  55.                     printfn "Please pay %i DKK for your %i portions" (getPriceForBag (bag) * qty) qty
  56.                 | LeaveAComment (comm) ->
  57.                     match comm with
  58.                     | "delicious" -> printfn "The food was amazing!!"
  59.                     | "satisfied" -> printfn "The food was good..!"
  60.                     | _ -> printfn "unsatisfied (%s)" comm
  61.                 return! processFood msg
  62.             }
  63.         processFood (LeaveAComment("initial")))
  64.  
  65. [<EntryPoint>]
  66. let main argv =
  67.     printfn "Hello World from F#!"
  68.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement