【发布时间】:2020-03-01 04:30:24
【问题描述】:
type Sizes =
| Big
| Medium
| Small
;;
//defines cup/can/bottle with size
type Containment =
| CupDrink of s:Sizes
| CannedDrink of s:Sizes
| BottledDrink of s:Sizes
;;
// defines record for each type for drink
type Coffee = {DrinkName : string; price: double }
type Soda = {DrinkName : string; price: double }
type Brew = {DrinkName : string; price: double }
// union for type of drink
type liquid =
| Coffee of c:Coffee
| Cola of s:Soda
| Beer of b:Brew
;;
let Guiness = Beer {DrinkName = "Guiness"; price = 0.15}
let CocaCola = Cola {DrinkName = "Cola"; price = 0.15}
let smallCup = CupDrink Small // it could be just containment | size
let bigBottle= BottledDrink Big
let findPricePrML(dr:liquid) =
let price = 0.0
match dr with
|Beer(b=h)-> h.price
|Cola(s=h) ->h.price
|Coffee(c=h) -> h.price
|_-> failwith "not found"
// returns size in ML fro each size available // asuming that small bottle , can and cup have same size,
//if not another kind of program can be made but it's not part of the assingment
let find size =
match size with
|Big -> 250.00
|Medium -> 125.00
|Small -> 75.00
let grandTotal (dr:liquid ,cont:Containment) = function
(*let temp = {dra=dr;conta = cont} //supossed to search on menu list if such item exists (can't figure the syntax)
if List.contains temp menuList then *)
|CupDrink (s=z) -> findPricePrML dr * find z
|BottledDrink (s=z) -> findPricePrML dr * find z
|CannedDrink (s=z) -> findPricePrML dr * find z
|_-> failwith "not found"
(* else failwith "no such item exists"*)
;;
let source = [|(CocaCola, bigBottle); (CocaCola, smallCup); (Tuborg, smallCup)|]
let Test =
Async.Parallel [ async { return Array.map grandTotal source } ]
|> Async.RunSynchronously
您好,我正在尝试学习 F# 中 CPU 绑定并行编程的基础知识。在这里,我有一个计算饮料价格的函数。我想要的只是将另一个函数(将结果乘以某个数字)应用于我从并行计算中获得的结果,但我不断收到类型不匹配错误。在我的解决方案中,我得到的结果是一个锯齿状数组。不幸的是,我也不知道如何以数组的形式获得结果。
【问题讨论】: