
Hi, I have been using Haskell for a few weeks now and I can say that I am totally impressed by how easy it is to program with it. I am now doing some small exercises to get used to the language syntax and I have a little (newbie) question: Suppose I declare a function foo like: foo :: [Double] -> Int -> Int foo a b Suppose now that I want to apply this function to a list that contains lists of doubles (something like [[Double]]) using map, but I want to keep the 'b' parameter fixed (with a value of 5 for instance). Is it possible to use map passing the function foo with the 2nd argument only ? In other words, if I wrote this function like: foo :: Int -> [Double] -> Int I could clearly call it with: map (foo 5) my_list_of_lists_of_doubles But how to do that (if possible) when I invert the parameters list ?! Thanks in advance, Andre Abs da Cruz

There is the "flip" function which changes the order of the first 2 parameters Prelude> :t flip flip :: forall c a b. (a -> b -> c) -> b -> a -> c So I think map ( (flip foo) 5 ) my_list_of_lists_of_doubles will work, as will using a lambda expression map (\x -> foo x 5) may_list_of_lists_of_doubles André Vargas Abs da Cruz wrote:
Hi,
I have been using Haskell for a few weeks now and I can say that I am totally impressed by how easy it is to program with it. I am now doing some small exercises to get used to the language syntax and I have a little (newbie) question:
Suppose I declare a function foo like:
foo :: [Double] -> Int -> Int foo a b
Suppose now that I want to apply this function to a list that contains lists of doubles (something like [[Double]]) using map, but I want to keep the 'b' parameter fixed (with a value of 5 for instance). Is it possible to use map passing the function foo with the 2nd argument only ? In other words, if I wrote this function like:
foo :: Int -> [Double] -> Int
I could clearly call it with:
map (foo 5) my_list_of_lists_of_doubles
But how to do that (if possible) when I invert the parameters list ?!
Thanks in advance, Andre Abs da Cruz _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

That's exactly what i was looking for. Thank you very much !!! Cheers, André ChrisK wrote:
There is the "flip" function which changes the order of the first 2 parameters
Prelude> :t flip flip :: forall c a b. (a -> b -> c) -> b -> a -> c
So I think map ( (flip foo) 5 ) my_list_of_lists_of_doubles will work, as will using a lambda expression map (\x -> foo x 5) may_list_of_lists_of_doubles
André Vargas Abs da Cruz wrote:
Hi,
I have been using Haskell for a few weeks now and I can say that I am totally impressed by how easy it is to program with it. I am now doing some small exercises to get used to the language syntax and I have a little (newbie) question:
Suppose I declare a function foo like:
foo :: [Double] -> Int -> Int foo a b
Suppose now that I want to apply this function to a list that contains lists of doubles (something like [[Double]]) using map, but I want to keep the 'b' parameter fixed (with a value of 5 for instance). Is it possible to use map passing the function foo with the 2nd argument only ? In other words, if I wrote this function like:
foo :: Int -> [Double] -> Int
I could clearly call it with:
map (foo 5) my_list_of_lists_of_doubles
But how to do that (if possible) when I invert the parameters list ?!
Thanks in advance, Andre Abs da Cruz _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- ------------------------------------------------------- André Vargas Abs da Cruz Laboratório de Inteligência Computacional Aplicada Departamento de Engenharia Elétrica Pontifícia Universidade Católica - Rio de Janeiro http://www.ica.ele.puc-rio.br/

So I think map ( (flip foo) 5 ) my_list_of_lists_of_doubles will work, as will using a lambda expression map (\x -> foo x 5) may_list_of_lists_of_doubles
I really like the `foo` syntax. map (`foo` 5) my_list also works, without an auxiliary function and without a lambda. In reality this gets de-sugared to a flip, but i prefer this syntax. `foo` uses foo as an infix operator. Thanks Neil

Hello André, Friday, September 16, 2005, 10:55:16 PM, you wrote: AVAdC> map (foo 5) my_list_of_lists_of_doubles AVAdC> But how to do that (if possible) when I invert the parameters list ?! third, most iniversal solution is to create anonymous lambda function: map (\x->foo x 5) my_list_of_lists_of_doubles of course, it is not needed for such trivial example, really i also prefer `foo` for this case -- Best regards, Bulat mailto:bulatz@HotPOP.com

Hi, Andre,
map (foo 5) my_list_of_lists_of_doubles
...But how to do that (if possible) when I invert the parameters list ?!
Let me add one more solution, and then summarize: The problem disapears if you use a list comprehension instead of map: [foo x 5 | x <- my_list_of_lists_of_doubles] List comprehensions are funny. Sometimes, they are powerful, consise, and crystal clear. Other times they can seem wordy and confusing. In this case it works well. OK, so the four solutions that have been suggested are: 1. map (flip foo 5) my_list_of_lists_of_doubles 2. map (`foo` 5) my_list_of_lists_of_doubles 3. map (\x->foo x 5) my_list_of_lists_of_doubles 4. [foo x 5 | x <- my_list_of_lists_of_doubles] (1) and (2) are closest to what you originally wanted to write. They do not neatly generalize beyond the second parameter. (3) is more general - but a little less clear. (4) is the most different from your original idea. In a certain sense, it is even more general than (3). In this case, it is also simple and clear - though you may not always be so lucky. BTW - great question! Regards, Yitz
participants (6)
-
André Vargas Abs da Cruz
-
Bulat Ziganshin
-
ChrisK
-
Henning Thielemann
-
Neil Mitchell
-
Yitzchak Gale