Search code examples
haskellpointfree

Why isn't it necessary to provide a parameter in this function?


I'm fairly new to Haskell and this week I found this particular function in a couple of lecture slides. I'm trying to understand why the following function doesn't need to include a parameter:

-- Return all final segments of the argument, longest first 
-- (horrible runtime complexity, used here for illustration purposes only)
tails :: [a] -> [[a]]
tails = reverse . map reverse . inits . reverse

If I would call it like tails "thisisastring" then this would be a valid argument. Isn't it necessary to provide a parameter, for example tails xs = .... All the other functions I seen before were in that fashion.


Solution

  • The parameter is implicit. Or to put it differently, reverse . map reverse . inits . reverse evaluates to a function of type [a] -> [[a]].

    Consider a simpler example:

    double_impl x = x * 2
    double = double_impl
    

    The type of double here is the same type as double_impl, i.e. it takes one parameter of typeclass Num:

    main = do 
      print $ double_impl 5
      print $ double 5
    
    -- Out: 10
    -- Out: 10