Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map list of functions on list in Haskell

Tags:

haskell

map

I have a problem with implementing FP (Backus) interpreter in Haskell.

FP functions look like this:

[+,*]:<2,3>

should result with

<+:<2,3>, *:<2,3>> ->
<5,6>

meaning that every function from list on left side should be performed on every every element of a list on a right side.

What I understand is that I need something similar to "map" function, but map applies one function on a list, and I need list of functions on a list of values.

Thank you in advance! :-)

EDIT:

Since I wasn't precise, here is my code that doesn't work:

apply :: [String] -> [Integer] -> [Integer]
apply fs v = [((apply_f f x) | f <- fs | x <- v)]

apply_f :: String -> [Integer] -> [Integer]
apply_f "+" v = [(sum v)]
apply_f "*" v = [(product v)]

I can't figure out why... :-(

EDIT 2:

Sorry, I was too tired from working all day on this. Problem is that I don't need second pipe, just first one:

apply fs v = [ apply_f f v | f <- fs ]

Now, everything works just fine, thank you very much! :-)

like image 347
markoub Avatar asked Aug 25 '12 17:08

markoub


2 Answers

I believe you are looking for the zipWith function and apply it with the function application operator $.

So, if you have a list of functions funcList and a list of values valueList you would call this with:

zipWith ($) funcList valueList

So, using this would be something like

zipWith ($) [(+ 5),(* 3)] [1,5]

gives you the result [6,15]

To get a sort of cross application you could use

[f a | f <- [(+5), (*3)], a <- [1,5]]

this gives you [6,10,3,15]. Not sure what you mean by the `<, do you want pairs, nested lists or what exactly do you need?

like image 153
Dervall Avatar answered Oct 26 '22 17:10

Dervall


It sounds like you want:

import Control.Applicative
apply :: [a -> b] -> [a] -> [b]
apply fs vals = fs <*> vals

Of course this is just the same as the instance of <*> defined for lists.

like image 40
Lee Avatar answered Oct 26 '22 15:10

Lee