1

i have a list with functions and one other list with "arguments" to make a new list where each element of the one list, map with the other element of the other list. (apply :: Ord u => [v->u]->[v]->[u] ) For example,

apply [(^2),(^3),(^4),(2^)] [10] = [100,1000,1024,10000]. or 
apply [reverse,(++"ing"),reverse.(++"ing"),(++"ing").reverse] ["play","do"] = ["doing","gniod","gniyalp","od","oding","playing","yalp","yalping"]..

What can i do, because i do my first steps in haskel..

1 Answer 1

4

Let us take your first list:

[(^2),(^3),(^4),(2^)]

It's type is xs :: Integral a => [a -> a]

Now you want to apply it to to a list [10]. What you want is exactly Applicative function <*> whose type is Applicative f => f (a -> b) -> f a -> f b:

λ> import Control.Applicative
λ> let xs = [(^2),(^3),(^4),(2^)]
λ> xs <*> [10]
[100,1000,10000,1024]

You can work out the types to see how they fit together. Your second example doesn't seem to be correct as you are not passing any second parameter to your apply function. I would suggest you to start reading LYAH to further solidify the concepts.

Sign up to request clarification or add additional context in comments.

10 Comments

This will apply every argument with every function, right? OP wants "each element of the one list, map with the other element of the other list" which I think can be understood as applying first function with first parameter only and so forth.
@user2079303 I had difficult parsing the question. If that's what OP what then he can take zip of both list and then apply them. But the expected output in his current question is wrong then ?
I just corrected my question (2nd parameter to my apply) .. is it more clear now? @sibi i want to apply every argument with every function..right
@ΔιονυσιαΑγαλιώτη Since you have corrected your second part of the question, why don't you apply that parameter to the <*> and see what it produces.
Ah, it seems my parsing was less accurate :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.