How can I bind the second argument in a function but not the first (in an elegant way)?

Posted by Frank Osterfeld on Stack Overflow See other posts from Stack Overflow or by Frank Osterfeld
Published on 2010-12-29T10:48:04Z Indexed on 2010/12/29 11:53 UTC
Read the original article Hit count: 123

Is there a way in Haskell to bind the second argument but not the first of a function without using lambda functions or defining another "local" function?

Example. I have a binary function like:

sub :: Int -> Int -> Int
sub x y = x - y 

Now if I want to bind the first argument, I can do so easily using (sub someExpression):

mapSubFrom5 x = map (sub 5) x

*Main> mapSubFrom5 [1,2,3,4,5]
[4,3,2,1,0]

That works fine if I want to bind the first n arguments without "gap".

If I want to bind the second argument but not the first, the two options I am aware of are more verbose:

Either via another, local, function:

mapSub5 x = map sub5 x
    where sub5 x = sub x 5

*Main> mapSub5 [1,2,3,4,5]
[-4,-3,-2,-1,0]

Or using lambda:

mapSub5 x = map (\x -> sub x 5) x

While both are working fine, I like the elegance of "sub 5" and wonder if there is a similarly elegant way to bind the n-th (n > 1) argument of a function?

© Stack Overflow or respective owner

Related posts about haskell

Related posts about functional-programming