0

I have a confusion between higher-order function and partially applied function. Can I say the partially applied function is Higher-order function because partially applied function also return function?

Tausif Sayyad
  • 248
  • 1
  • 9

2 Answers2

1

Higher order function is the fact that you can pass one or multiple functions as parameters or have them as a result. Functions are the same as variables. whereas currying is when a function has multiple parameters and you precise only a few of them so you obtain a new function. So currying can be seen as an example of higher order function because you return a function when you use currying. Currying example:

def plus(a :Int)(b: Int): Int = a + b
def plus5 = plus(5)

plus5 is a function and that's a kind of higher order function.

firsni
  • 856
  • 6
  • 12
  • "Higher order function is the fact that you can pass one or multiple functions as parameters or have them as a result" Higher order function is that's it's possible to pass a function B as argument of function A. – cchantep Sep 23 '19 at 07:59
  • And what about partially applied function? Is it also a higher-order function? – Tausif Sayyad Sep 23 '19 at 08:21
  • a partial function is a function of `T` not able to handle all the possible `T` values (not defined for the whole domain/category) – cchantep Sep 23 '19 at 08:23
  • @cchantep sorry I am talking about partially applied function. – Tausif Sayyad Sep 23 '19 at 08:24
  • partially applied function is related to [currying](https://stackoverflow.com/questions/36314/what-is-currying); `def foo(a: Int, b: Int) = a + b; val bar: Int => Int = foo(5, _: Int)` – cchantep Sep 23 '19 at 08:46
  • Partially applied function is the same as currying – firsni Sep 23 '19 at 11:44
  • You mean both currying and partially applied function are not higher-order functions right? – Tausif Sayyad Sep 23 '19 at 14:06
  • No Sorry, I meant they are kind of Higer order function because they output a method. I'll try to make my answer clearer – firsni Sep 24 '19 at 07:35
1

Partially applied function is the result of the process of partial application. This resulting function does not necessarily have to be higher-order, for example consider

val f: (Int, Int) => (Int) = (a, b) => a + b
val g: Int => Int = f(1, _) //  g is partially applied function

Here function g is a partially applied function and yet it is first-order Int => Int. However the process of partial application itself is indeed a higher-order process as it takes a function and returns a function, similar to, perhaps, differential operator from calculus which also takes a function as input and returns a function as output.

As a side-note, currying and partial application are related but not identical.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98