【问题标题】:Applying a function on an F# array在 F# 数组上应用函数
【发布时间】: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 绑定并行编程的基础知识。在这里,我有一个计算饮料价格的函数。我想要的只是将另一个函数(将结果乘以某个数字)应用于我从并行计算中获得的结果,但我不断收到类型不匹配错误。在我的解决方案中,我得到的结果是一个锯齿状数组。不幸的是,我也不知道如何以数组的形式获得结果。

【问题讨论】:

    标签: arrays f#


    【解决方案1】:

    我认为您的代码的第一个问题是您的 grandTotal 函数需要两个参数:

    let grandTotal (dr:liquid, cont:Containment) = function
      | ...
    

    这意味着您必须使用grandTotal (CocaCola, bigBottle) drinkKind 之类的名称来调用它。但是,在您尝试调用它的代码中,您使用:

    Array.map grandTotal source
    

    这调用grandTotal 时只有一个参数——source 列表中的一个项目,所以你得到的是一个函数而不是一个价格。你可能需要这样的东西:

    Array.map (fun drink -> grandTotal drink kind) source
    

    第二个问题是你并没有真正并行化任何东西。使用async 的方式,您只是创建一个计算,然后在后台线程上运行它。你可以这样做:

    let test =
        [ for a in source -> async { return grandTotal a kind } ]
        |> Async.Parallel 
        |> Async.RunSynchronously
    

    不过,更高效、更简单的方法是使用Array.Parallel.map

    Array.Parallel.map (fun drink -> grandTotal drink kind) source
    

    要回答您关于调用另一个函数的问题 - 如果不查看我们可以运行的更完整的代码示例,这是不可能的。

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 2012-05-26
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多