0

I'm new to Haskell and I was wondering how would I be able to keep track of the index when it comes to higher order classes I'm trying to build a function that takes a [a] list and a function (a->Int->b) which essentially would be f(ai, i) and returns list [b].

I thought of doing something like:

higherOrd :: [a]->(a->Int->b)->[b] higherOrd t f = [f x y | x <- t, y <- [1..length(t)]] 

for example, if I do higherOrd (1,2..10) (+) I get:

[2,3,4,5,6,7,8,9,10,11,3,4,5,6,7,8,9,10,11,12,4,5,6,7,8,9,10,11,12,13,5,6,7,8,9,10,11,12,13,14,6,7,8,9,10,11,12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,11,12,13,14,15,16,17,18,19,20] 

when I would want to get [2,4,6,8,10,12,14,16,18,20]

0

1 Answer 1

0

You use zip:

higherOrd :: [a] -> (a -> Int -> b) -> [b] higherOrd t f = [f x y | (x,y) <- zip f [1..]] 

this then enumerates over the two lists at the same time. Or we work with a function that Haskell already provides: zipWith:

higherOrd :: [a] -> (a -> Int -> b) -> [b] higherOrd t f = zipWith f t [1..] 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.