What I'm missing is the ability to partially apply the second argument of a function rather than the first. This is especially useful when I want to pass the function to something like map, but without having to write a lambda for it each time.
I wrote my own function for this (definition below, just in case there indeed isn't any built-in function for this and anyone else was curious), but I would really like to know if there already exists something in the Prelude for this idiom as I prefer to reuse rather than reinvent.
Here is my definition and a trivial example:
bind2nd :: (a -> b -> c) -> b -> a -> c
bind2nd f b = \a -> f a b
foo :: Int -> Bool -> String
foo n b | b = show n
| otherwise = "blabla"
alwaysN :: Int -> String
alwaysN = bind2nd foo True
It's called flip
.
Example:
Prelude> flip (-) 2 3
1
For future reference, it could have been found by simply searching Hoogle for the type signature in your question, namely (a -> b -> c) -> b -> a -> c. :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With