0

Hello I have a function foo (Int -> Int -> Int) in haskell and I want to map each element of a list [1..y] to a value x which I would usually do like this:

map (foo x) [1..y]

the problem is my function is not commutative so what I actually want to do is something like this (careful incorrect syntax ahead):

map (foo [1..y]) x

So basically I want to pass my function each element of a list and map each element to x but the values [1..y] need to be on the left side of the function. Note that foo takes two Ints as arguments. I am sure haskell has some trick in its repository for this, can you help me out :) ?

CoffeeKid
  • 123
  • 5

2 Answers2

1

Consider the following example:

λ> map (flip (-) 3) [1..10]
[-2,-1,0,1,2,3,4,5,6,7]

flip takes a function of type a -> b -> c and two arguments b and a (in that order) and applies the function to the arguments in flipped order.

Another way that comes to mind is used zip and an infinite list of x (3 in this example) to create pairs of arguments that you want you function applied to.

user1984
  • 5,990
  • 2
  • 13
  • 32
0

The function "flip" out of the prelude did the trick for me :)

map (flip foo x) [1..y]
CoffeeKid
  • 123
  • 5