F#柯里化函数 [英] F# curried function

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

问题描述

谁有一个像样的例子,最好是实用/有用的,他们可以发布演示这个概念吗?

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

推荐答案

(一个小的 Ocaml FP Koan 开始)

咖喱公案(关于食物的公案,与食物无关)

一位学生来到雅克·加里格 (Jacques Garrigue) 说:我不明白咖喱有什么好处."雅克回答说:告诉我你最喜欢的一餐和你最喜欢的甜点".困惑的学生回答说他喜欢御好烧和kanten,但是虽然他最喜欢的餐厅提供很棒的御好烧,但他们的kanten第二天早上总是让他胃痛.因此,雅克带学生去一家餐厅,餐厅供应的御好烧和学生最喜欢的一样好,然后带他穿过镇子,去了一家制作优质kanten 的商店,学生很高兴地把剩下的胃口都用了.学生吃饱了,但他没有开悟……直到第二天早上醒来,他的胃感觉很好.

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

但这与:

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

好的,现在 rowland 和 cloitre 是柯里化函数,因为它们有自由变量,我们可以得到它的序列的任何索引,而无需知道或担心 f_recurrence.

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天全站免登陆