为什么要更高层次的程序? [英] Why higher order procedures?
问题描述
所以如果一种语言提供了更高阶的过程,那么我可以有返回过程的过程。例如:
So if a language provides higher order procedure then I can have procedure that returns procedure. Something like:
(define (Proc a b c)
(lambda (x) ( #| method body here in terms of a b c and x |# )))
要创建新的过程,
(define ProcA (Proc a1 b1 c1)) ; Would create ProcA that has 1 argument
类似的任务可以在不支持高阶通过定义 Proc
,它需要4个而不是3个参数,并调用此过程来定义 ProcA
,如:
Similar task could be done in a language which does not support higher order procedure by defining Proc
that takes 4 instead of 3 arguments and calling this procedure to define ProcA
, like:
(define (Proc a b c x) ( #| method body -- does not return any procedure |# )
(define (ProcA x) (Proc a1 b1 c1 x))
那么为什么有更多的fuzz
So why is there so much fuzz about higher order procedure? Am I missing something?
推荐答案
这是一个很好的观察,一个函数返回另一个函数是相同的函数,换句话说,从A到B的函数是逻辑意义的证明,A意味着B,或者:
It's a good observation that a function that returns another function is the same as a function that takes two arguments. This is called "Currying". Put another way, a function from A to B is proof of a logical implication, that A implies B, or:
A => B.
B表示C,则A和B表示C,或者:
As you note, if A implies that B implies C, then A and B implies C, or:
(A => (B => C)) <==> ((A, B) => C)
但是高阶函数不一定是函数返回另一个函数。 高阶函数是以另一个函数作为其参数的函数。这是一个重要的区别,HOFs是非常强大的编程工具。
But a higher order function is not necessarily a function that returns another function. A higher-order function is a function that takes another function as its argument. This is an important difference, and HOFs are immensely powerful programming tools.
例如,考虑这个Haskell函数:
For example, consider this Haskell function:
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : (map f xs)
这个高阶函数使用一个函数 f
并将其应用于列表中的每个元素。在没有HOF的语言中,你可以用循环或类似的东西做这个函数,但是在有HOF的语言中,你可以为列表中的每个元素调用 f
用这样的简单调用:
This higher-order function takes a function f
and applies it to every element in a list. In languages without HOFs, you would do what this function does with a loop or something similar, but in a language that has HOFs, you can call f
for every element in the list with a simple call like this:
map f myList
当然,语言中的控制结构可以逼近高阶函数,但具有高阶函数的语言可让您发明自己的控制结构 。计划肯定有资格。
Sure, control constructs in languages let you approximate higher-order functions, but a language that has higher-order functions lets you invent your own control constructs. Scheme certainly qualifies.
这篇关于为什么要更高层次的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!