how can i get multiple nth elements in a list?

You can work with a mapping where you lookup all items for a list, so:

map (a !!) [0,1,2,3]

If you are however interested in the first four items, you can work with take :: Int -> [a] -> [a]:

take 4 a

especially since looking up by an index (with (!!)) is not a common operation in Haskell: this is unsafe since it is not guaranteed that the index is in bounds. Most list processing is done with functions like take, drop, sum, foldr, etc.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top