haskellpointfree

In Haskell how to make a function expression "point-free"


I have a Haskell function like this:

in2out :: String -> String
in2out s = 
  show (sumAllLineValues $ lines s)

(where sumAllLineValues is defined in my code.)

How can I define in2out point-free, so without the parameter s?


Solution

  • To make a function pointfree, you need to rearrange the uses of all of its arguments such that they can be eta-reduced away. In this case, the argument is already used exactly once and all the way on the right, so all you need to do is get it out of the parentheses, which you can do by using function composition (.):

    in2out :: String -> String
    in2out s = 
      (show . sumAllLineValues . lines) s
    

    And then eta-reduce it away:

    in2out :: String -> String
    in2out = 
      show . sumAllLineValues . lines