higher order functions in haskell

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..

Upvotes: 1

Views: 97

Answers (1)

Sibi
Sibi

Reputation: 48644

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.

Upvotes: 4

Related Questions