F#咖喱功能 [英] F# curried function

查看:184
本文介绍了F#咖喱功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有一个体面的例子,最好是实用的/有用的,他们可以发布展示的概念?

解决方案


(编辑:小 Ocaml FP Koan 启动)


Currying(A koan关于食物,这与食物无关)


一位学生来到Jacques Garrigue说:我不明白咖喱很好。雅克回答说,告诉我你最喜欢的餐点和你最喜欢的甜点。这位疑惑的学生回答说,他喜欢大阪烧和卡宁,但是当他最喜欢的餐厅供应很棒的大阪烧时,他们的筷子总是在第二天早上给他肚子疼。所以雅克带着学生在一家餐厅里吃了大阪烧,然后把他带到了一家商店,那家餐馆做得非常出色,在那里学生们愉快地运用了剩下的食欲。这个学生是坐着的,但他没有开悟......直到第二天早上他醒来时,他的肚子感觉很好。




我的示例将使用它来进行重用和封装的代码。这是相当明显的,一旦你看这些,应该给你一个具体的,简单的例子,你可以想到在许多情况下申请。



我们想要做一个地图一颗树。如果需要多于一个参数的话,这个函数可以被curry应用到每个节点上 - 因为我们会在节点上应用这个函数作为最终的参数。它不需要被curry,但是编写另一个函数(假设这个函数在其他变量的其他实例中被使用)将是浪费。

 键入'a tree'的'a' 'a *'一棵树*'一棵树
让rec tree_map f tree =与
匹配的树| N(x,左,右) - > N(f x,tree_map f left,tree_map f right)
| E(x) - > E(fx)

let sample_tree = N(1,E(3),E(4)
让multiply xy = x * y
let sample_tree2 = tree_map(multiply 3 )sample_tree

但这与以下内容相同:

sample_tree

所以这个简单的例子并不令人信服,一旦你使用了更多的语言并自然地遇到了这些情况,它确实是强大的,另一个例子是一些代码重用为currying。A 发生关系以创建素数。在那里非常相似: p>

  let rec f_recurrence fa种子n = 

匹配n - >种子
| $ - $ gt; $ - $> let prev = f_recurrence fa seed(n-1)in
prev +(fn prev)

let rowland = f_recurrence gcd 1 7
let cloitre = f_recurrence lcm 1 1

让rowland_prime n =(rowland(n + 1)) - (rowland n)
let cloitre_prime n =((cloitre(n + 1))/(cloitre n)) - 1

Anyone have a decent example, preferably practical/useful, they could post demonstrating the concept?

解决方案

(Edit: a small Ocaml FP Koan to start things off)

The Koan of Currying (A koan about food, that is not about food)

A student came to Jacques Garrigue and said, "I do not understand what currying is good for." Jacques replied, "Tell me your favorite meal and your favorite dessert". The puzzled student replied that he liked okonomiyaki and kanten, but while his favorite restaurant served great okonomiyaki, their kanten always gave him a stomach ache the following morning. So Jacques took the student to eat at a restaurant that served okonomiyaki every bit as good as the student's favorite, then took him across town to a shop that made excellent kanten where the student happily applied the remainder of his appetite. The student was sated, but he was not enlightened ... until the next morning when he woke up and his stomach felt fine.

My examples will cover using it for the reuse and encapsulation of code. This is fairly obvious once you look at these and should give you a concrete, simple example that you can think of applying in numerous situations.

We want to do a map over a tree. This function could be curried and applied to each node if it needs more then one argument -- since we'd be applying the one at the node as it's final argument. It doesn't have to be curried, but writing another function (assuming this function is being used in other instances with other variables) would be a waste.

type 'a tree = E of 'a | N of 'a * 'a tree * 'a tree
let rec tree_map f tree = match tree with
    | N(x,left,right) -> N(f x, tree_map f left, tree_map f right)
    | E(x) -> E(f x)

let sample_tree = N(1,E(3),E(4)
let multiply x y = x * y
let sample_tree2 = tree_map (multiply 3) sample_tree

but this is the same as:

let sample_tree2 = tree_map (fun x -> x * 3) sample_tree

So this simple case isn't convincing. It really is though, and powerful once you use the language more and naturally come across these situations. The other example with some code reuse as currying. A recurrence relation to create prime numbers. Awful lot of similarity in there:

let rec f_recurrence f a seed n =
    match n with
    | a -> seed
    | _ -> let prev = f_recurrence f a seed (n-1) in
           prev + (f n prev)

let rowland = f_recurrence gcd 1 7
let cloitre = f_recurrence lcm 1 1

let rowland_prime n = (rowland (n+1)) - (rowland n)
let cloitre_prime n = ((cloitre (n+1))/(cloitre n)) - 1

Ok, now rowland and cloitre are curried functions, since they have free variables, and we can get any index of it's sequence without knowing or worrying about f_recurrence.

这篇关于F#咖喱功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆