Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in Haskell equivalent for C++'s std::bind2nd?

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
like image 568
Michel Avatar asked Dec 08 '22 08:12

Michel


1 Answers

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. :-)

like image 157
ShreevatsaR Avatar answered Dec 09 '22 22:12

ShreevatsaR