【问题标题】:How do I create a collection of functions?如何创建函数集合?
【发布时间】:2016-04-26 22:27:57
【问题描述】:

我只想说“usd_to_euro 100”并为我的特定日子取回正确金额的欧元(例如:90)。 create_exchange_functions 需要返回什么?

let create_exchange_functions (usd_to_euro_rate, usd_to_yuan_rate, usd_to_peso_rate, usd_to_pound_rate, usd_to_ruble_rate) = 
    (??, ??, ??, ....??);

//在第 x 天早上我运行这个:

let (usd_to_euro, usd_to_yuan, usd_to_peso, usd_to_pound, usd_to_ruble) = create_exchange_functions(0.9, 4., 3., 0.6, 5.);
usd_to_pound 10.;

//在第 y 天的早上我运行这个:

let (usd_to_euro, usd_to_yuan, usd_to_peso, usd_to_pound, usd_to_ruble) = create_exchange_functions(0.92, 3.8, 3., 0.65, 5.);
usd_to_pound 10.;

//在第 z 天的早上我运行这个:

let (usd_to_euro, usd_to_yuan, usd_to_peso, usd_to_pound, usd_to_ruble) = create_exchange_functions(0.92, 3.8, 3., 0.62, 5.);
usd_to_pound 10.;

【问题讨论】:

  • 为什么要在巨大的元组中搞砸呢? - 显然,您可以使用let inline exchange rate input = input * rate 之类的汇率进行操作,然后使用let exchangeFuns = rates |> List.map exchange 将汇率列表映射到交换函数列表中
  • 如果您使用度量单位,这也可以非常易读且非常安全
  • 我正在尝试在不使用列表函数的情况下处理语言。试图更好地理解更基础的内容。
  • 您是使用 F# Interactive 还是编写代码和编译?如果您想从最基本的层面学习并避免使用列表,那么请使用 F# Interactive 创建一些函数并使用 F# Interactive 快速处理它们。
  • 在视觉工作室上进行交互。它帮助我尝试使用模式匹配来解决这个问题。我几乎想通了,但总是欢迎帮助。

标签: design-patterns f# functional-programming currying


【解决方案1】:

给定

let create_exchange_functions (usd_to_euro_rate, usd_to_yuan_rate, usd_to_peso_rate, usd_to_pound_rate, usd_to_ruble_rate) = 
(??, ??, ??, ....??);

如何做到这一点。

让我们先简化一个项目,然后我们将完成所有项目。

let create_exchange_functions usd_to_euro_rate = 
    let toEuro dollar = usd_to_euro_rate * dollar
    toEuro

理解你想要什么的关键是你想要一个返回一个新函数的函数,该函数接受一个curried 值。特别是对于这个例子,柯里化参数是函数需要但在创建函数时没有给出的参数。

要查看此内容,请将类型添加到 create_exchange_functions

let create_exchange_functions (usd_to_euro_rate : float) : (float -> float) = 
    let toEuro dollar = usd_to_euro_rate * dollar
   toEuro

我们看到create_exchange_functions 接受一个值usd_to_euro_rate 并返回一个函数(float -> float),这正是我们想要的。

但是请注意,当我们创建函数toEuro 时,我们只给它usd_to_euro_rate 的值,而不是dollar 的值。这就是柯里化正在做的事情。它允许我们创建需要参数的函数,并且可以稍后提供参数。那么让我们看看这是如何做到的。

首先使用create_exchange_functions创建函数usd_to_eruo

let usd_to_euro = create_exchange_functions 0.9

注意usd_to_euro的签名是

val usd_to_euro : (float -> float)

所以如果我们给它一个浮点值,它会给我们另一个浮点值。 我们给它的价值是美元的价值

let dollar = 10.0

我们像使用它一样使用它

let euro = usd_to_euro dollar
printfn "Dollar: %A to Euro: %A" dollar euro

这给了

Dollar: 10.0 to Euro: 9.0

这是我们想要的。

现在为所有汇率做这件事

let create_exchange_functions (usd_to_euro_rate, usd_to_yuan_rate, usd_to_peso_rate, usd_to_pound_rate, usd_to_ruble_rate) = 
    let toEuro dollar = usd_to_euro_rate * dollar
    let toYuan dollar = usd_to_yuan_rate * dollar
    let toPeso dollar = usd_to_peso_rate * dollar
    let toPound dollar = usd_to_pound_rate * dollar
    let toRuble dollar = usd_to_ruble_rate * dollar
    (toEuro, toYuan, toPeso, toPound, toRuble)

let (usd_to_euro, usd_to_yuan, usd_to_peso, usd_to_pound, usd_to_ruble) = 
    create_exchange_functions(0.9, 4., 3., 0.6, 5.)

let dollar = 10.0
let pound = usd_to_pound dollar
printfn "Monday - Dollar: %A to Pound: %A" dollar pound

Monday - Dollar: 10.0 to Pound: 6.0

let (usd_to_euro, usd_to_yuan, usd_to_peso, usd_to_pound, usd_to_ruble) = 
    create_exchange_functions(0.92, 3.8, 3., 0.65, 5.)

let pound = usd_to_pound dollar
printfn "Tuesday - Dollar: %A to Pound: %A" dollar pound

Tuesday - Dollar: 10.0 to Pound: 6.5

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-03-05
  • 2015-06-16
  • 1970-01-01
  • 2022-11-10
  • 2013-09-15
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多