Hacker News new | ask | show | jobs
by codethief 86 days ago
> Why does the author think it's confusing to partially apply foldr and map but not (+), (.), const, and length?

Neither of these functions is being partially applied in the code you cited. While the placeholder $ can be used to indicate all "free slots" of a function, you would only have to use it in partial applications of that function. (Analogous to how in mathematics the abstract index notation for tensors[0] is only really useful when you start contracting tensors, etc. Otherwise, the plain objects/tensors without indices are much easier to write & read.)

Specifically:

- (+) was already a function of two arguments, so usage of $ is unnecessary since (+)($, $) == (+). Similarly with the length function (a function of 1 argument): length($) == length.

- Function composition was being used as a binary operator between two functions. You just replaced the infix notation with prefix notation.

- I read const(1) as the function that maps everything to 1. I.e. `const` is a function of one argument x, which returns a function that always returns x. Once again, no need to indicate slots there.

[0]: See https://en.wikipedia.org/wiki/Abstract_index_notation and https://math.stackexchange.com/questions/455478/what-is-the-...

1 comments

> - Function composition was being used as a binary operator between two functions. You just replaced the infix notation with prefix notation.

That's incorrect. Here's the definition of function compose:

  (f . g) x = f (g x)
So in `foldr (+) 0 . map (const 1)`, the author gives `f = foldr (+) 0` and `g = map (const 1)` but doesn't supply `x`. That's a partial application. Similarly for const:

  const x y = x
Even if I concede length and (+), these two are partially applied.

> you would only have to use it in partial applications of that function.

So why not const and (.)? If they're allowed to curry, why not foldr and map?

> So in `foldr (+) 0 . map (const 1)`, the author gives `f = foldr (+) 0` and `g = map (const 1)` but doesn't supply `x`. That's a partial application.

It's not, and the authors doesn't have to. Using your definitions, `f . g` is a function of a single free argument (whether you call it x or y or whatever). Partial application occurs when you want to fix some parameters but not all of them. In the present case, however, there is only one parameter (x) and it's not fixed, so there's no partial application to speak of and one can omit the parameter as usual.

Put differently: (.) is a binary operator on functions, and length = f . g from the OP is an equation of functions. You can certainly write it down pointwise, i.e. length(x) = (f . g)(x), but that doesn't tell you anything you didn't know already, and it is unrelated to the case of partial application that the author is discussing.