高阶和​​咖喱函数之间的区别 [英] Difference between higher order and curried functions

查看:105
本文介绍了高阶和​​咖喱函数之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一本书,函数式编程使用F#高阶函数声明部分中<=> CDEQ6AEwAA#v = onepage& q = higher-order%20functions& f = falserel =nofollow noreferrer>说(第33页) (+)和(<<)

$这样的高阶内建函数, b $ b

并在该部分结尾处


高阶函数可以通过在let声明中提供参数来定义:

  let weight ro s = ro * s * * 3.0 ;; 


然而,在我今天早些时候问过的问题(最初名为我应该什么时候把我的函数写成高阶函数),这些函数似乎对这些例子实际上是否是高阶函数有疑问。

维基百科对高阶函数的定义是:



< blockquote>

高阶函数(也是函数形式,函数或仿函数)是一个至少执行以下操作之一的函数:(i)将一个或多个函数作为输入; (ii)输出一个函数。

一方面,我可以看到像(+)这样的函数, weight 可能被认为是高阶函数,因为给定一个参数它们会返回一个函数。另一方面,我可以看到他们被恰当地视为咖喱功能。我正在学习F#作为一个自学项目,并希望明确概念,所以本网站的答案和讨论特别有用。

我的问题是,这些功能的正确术语是什么,而且更重要的是,人们通常如何使用高阶函数和咖喱函数这两个术语?

解决方案

我认为你可以说curry函数是一个高阶函数,它返回一个函数作为结果。

curried函数是一个类似于 a - >的类型的函数。 b - > c - 如果添加括号(不改变类型) a - > (b - > c),您可以看到这也是更高阶的。



然而,您可以编写更高阶,但不会被咖喱。例如,下面的简单函数接受一些函数 f 并调用它两次:

  let runTwice f = f(); F(); 

该函数的类型为(unit - > unit) - >单位,所以它不会被curried(它只需要一些输入和返回单位值),但它是高阶的,因为参数是一个函数。



尽管像(+)这样的函数在技术上更高阶(类型是 int - >(int - > int)),我不认为它们是高阶的好例子,因为你通常不会以高阶方式使用它们(但偶尔会有用)。更高级的函数更典型的例子是像 List.map 这样的函数,它们将函数作为参数。


I'm reading a book, Functional Programming Using F#, which says (page 33), in the section Declaration of higher-order functions

We have seen higher-order built-in functions like (+) and (<<)

and at the end of the section

Higher-order functions may alternatively be defined by supplying the arguments as follows in the let-declaration:

let weight ro s = ro * s ** 3.0;;

However there were some helpful comments at the bottom of a question that I asked earlier today (which was originally titled "When should I write my functions as higher order functions") that seem to throw some doubt on whether these examples are in fact higher-order functions.

The wikipedia definition of higher-order function is:

a higher-order function (also functional form, functional or functor) is a function that does at least one of the following: (i) take one or more functions as an input; (ii) output a function.

On the one hand, I can see that functions like (+), and weight might be regarded as higher order functions because given a single argument they return a function. On the other hand I can see that they are properly regarded as curried functions. I'm learning F# as a self-study project and would like to get the concepts clear, so the answers and discussion on this site are particularly helpful.

My question is, what is the right term for these functions, and, perhaps more importantly, how do people normally use the terms "higher order functions" and "curried functions"?

解决方案

I think you could say that curried function is a higher-order function that returns a function as the result.

A curried function is a function with a type that looks like a -> b -> c - and if you add parentheses (which does not change the type) a -> (b -> c), you can see that this is also higher-order.

However, you can write functions that are higher-order but not curried. For example, the following simple function takes some function f and calls it twice:

let runTwice f = f(); f();

This function has a type (unit -> unit) -> unit, so it is not curried (it just takes some input and returns unit value), but it is higher-order because the argument is a function.

Although functions like (+) are technically higher-order (the type is int -> (int -> int)), I do not think that they are good examples of higher-order, because you do not usually use them in the higher-order way (but it is occasionally useful). More typical examples of higher-order functions are functions like List.map that take functions as arguments.

这篇关于高阶和​​咖喱函数之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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