Search code examples
haskelloperatorspointfree

function to write function after argument


I don´t like to read the order of functions from right to left in haskell. To fix this, I added a little useful operator.

module Main where
(>>>)            :: a -> (a -> b) -> b
(>>>) a fun      =  fun a 
main = print $ [1..10] >>> map (*2) >>> filter (>5) >>> foldr1 (+)
-- => 104

I think I could find here a similar built-in operator. Like bind operator (>>=). But bind operator works differently or to be exact I don't understand how it works. It seems it use concat map. But why?

My next point to improve that operator is make him called once. For example:

print $ [1..10] >>> map (*2) $ filter (>5) $ foldr (+)

I try to make it with (>>>) a = a and (>>>) a fun = (>>>) (fun a), but it seems that this overload is not possible. Clarify that I focused on the learning of functions and still don`t know anything about monads, types and classes.

So my question is: built in operator or correct using of bind operator.


Solution

  • "I don´t like to read the order of functions from right to left in haskell. To fix this, I added a little useful operator."

    "still don`t know anything about monads, types and classes"

    I don't think that trying to "fix" something in the language without understanding its basic concepts is a good idea.

    First of all, there are popular libraries and functions in Haskell providing what you want, e.g. lens's & operator does exactly what your >>> does. Secondly, the name >>> is already occupied by a Category's (of the base library) implementation, so it's not a good idea to reimplement it. Basically it is just a reverse of a composition operator ., but I suspect you're not well acquainted with function composition either. Thirdly, the bind (>>=) operator is too far in purposes from what you expect, you need to study monads to understand it.