1

Is it possible to manipulate nested function compositions in Haskell, in such a way that some processing be executed to each function before the composition?

Suppose a nested composition like (lines . unlines . words) "Testing Haskell composition" (it does not make sense; it is just an random example). I would like to do something before executing lines,unlines and words. Something like this fake code:

performComposition :: [functions] -> returnValue
performComposition [] = ()
performComposition (f:fs) = do something
                            f (performComposition fs )

Is it possible, even if it is necessary to create a new operator?

EDIT (February 17th, 2016)

I did not mention that i wanted to do something different on the last function. On the example above, the words function would do something different from the others before executing it.

The solution i have found is detailed below.

andregps
  • 341
  • 1
  • 7
  • you could write your own *composition* operator - but the `do something` part smells of sideeffects ... – Random Dev Feb 01 '16 at 13:35
  • 1
    Can you be a little more specific in your example? What are you wanting to use this for? What problem are you trying to solve that can't already be solved by simply using something like `run before computation after = before >> return computation >>= (\val -> after >> return val)`? This is a monadic function, though, so it can't be used in place of `computation`. – bheklilr Feb 01 '16 at 13:48
  • Wait: do you want this: `performComposition [] = id; performComposition (f:fs) = f . performComposition fs`? But this simply generalizes composition to a list of functions (it's a `fold`). – Bakuriu Feb 01 '16 at 14:45
  • There's nothing magic in composition. It is simply `f . g = \x -> f(g x)`. If you expect to be able to decompose this particular term, why not any other term? How about `2 + 2`? Given `4`, would you be able to determine it was `2 + 2` and not `3 + 1` originally? – n. m. could be an AI Feb 17 '16 at 14:40

2 Answers2

1

The solution i have found to my problem was to create something similar to a ternary operator. This link was essential.

So, a simple demonstration of the solution to my problem is:

(>=>) :: a -> (a->b) -> b   -- first operator
x >=> y = doSomething1  --fake code
          y x

(¨¨) :: a -> (a->b) -> b    -- second operator
x ¨¨ y = doSomething2  --fake code
         y x

infixl 0 >=> 
infixl 1 ¨¨ 

times2 :: Int -> Int 
times2 x = x*2

plus2 :: Int -> Int
plus2 x = x+2

plus3 :: Int -> Int
plus3 x = x+3

main = putStrLn $ show (3 ¨¨ plus2 ¨¨ times2 >=> plus3)
andregps
  • 341
  • 1
  • 7
0

Similar question with several answers can be found here. As I understand there is no build in mechanism for function composition splitting. You could add some kind of construct (more details in the link), but it would break haskells functional purity, because function decomposition should provide different result with different composition rules a=f.(g.h) and b=(f.g).h. (but its the same function that does identical transformation so it should return the same thing everytime)

Community
  • 1
  • 1
kodvin
  • 1,236
  • 9
  • 14
  • 2
    This looks like a comment. And if you think that the question is a duplicate you should vote/flag for duplication. – Bakuriu Feb 01 '16 at 15:02