List of function list in a list in Haskell

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

FP functions are as follows:

[+,*]:<2,3> 

should appear with

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

means that each function from the list on the left should be executed for each element of the list on the right.

I understand that I need something similar to the "map" function, but the map uses one function in the list, and I need a list of functions in the list of values.

Thank you in advance!: -)

EDIT:

Since I was not accurate, here is my code that does not work:

 apply :: [String] -> [Integer] -> [Integer] apply fs v = [((apply_f fx) | f <- fs | x <- v)] apply_f :: String -> [Integer] -> [Integer] apply_f "+" v = [(sum v)] apply_f "*" v = [(product v)] 

I can’t understand why ...: - (

EDIT 2:

Sorry, I'm too tired of working all day on this. The problem is that I do not need a second pipe, only the first:

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

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

+7
source share
5 answers

Do you need something like this?

 [fx | f <- [(+3),(*2)], x <- [1..2]] 

Output:

 [4,5,2,4] 

EDIT:

So something like this?

 [[fx | f <- [(+3),(*2)]] | x <- [1..2]] [[4,2],[5,4]] 
+6
source

It looks like you want:

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

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

+16
source

I believe that you are looking for the zipWith function and applying it to the $ function application operator.

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

 zipWith ($) funcList valueList 

So using this will be something like

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

gives the result [6,15]

To get a kind of cross-application, you can use

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

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

+13
source

I cannot understand anything from your explanation, but here is a possible code that gives the correct answer for your example:

 > map (uncurry foldr1) [((+), [2,3]), ((*), [2,3])] [5,6] 
0
source

I don’t know if you really need it, but

 Prelude> [f 2 3 | f <- [(+), (*)]] [5,6] 

If you need lists from <2,3> pairs, you can use something like

 Prelude> [zipWith f [2, 20] [3, 30] | f <- [(+), (*)]] [[5,50],[6,600]] 
-one
source

All Articles