3

For if I wanted to make a higher order function that filters even numbers out of a list using the predefined filter function I can do it like this:

filtereven list = filter even list

but another thing that still works is:

filtereven = filter even

why does this work and how does it know that it should expect an input list?

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
  • 1
    The word you're looking for is `currying`. Unlike most other programming languages, Haskell functions are "curried" by default, which makes usages like this possible (and natural). – Cubic May 10 '19 at 15:32
  • 9
    How does it know? Types! – augustss May 10 '19 at 16:17
  • 1
    (Specifically, the type of `filter`. `filter` is declared with (curried) type `(a -> Bool) -> [a] -> [a]`, which is the same as `(a -> Bool) -> ([a] -> [a])` (function type arrow `->` is right associative, so you can always add parentheses on its right side). Therefore `filter anyPredicate` must have type `[a] -> [a]`, which is a thing that expects an input list.) – user11228628 May 10 '19 at 17:08
  • 5
    You do this in English too. You can say "To teach is to understand" (`teach = understand`) without having to say "To teach something is to understand that thing" (`teach x = understand x`)" – that other guy May 10 '19 at 17:27

0 Answers0