How to get every Nth element of an infinite list in Haskell?

后端 未结 23 1324
再見小時候
再見小時候 2020-12-04 23:58

More specifically, how do I generate a new list of every Nth element from an existing infinite list?

E.g. if the list is [5, 3, 0, 1, 8, 0, 3, 4, 0, 93, 211, 0

23条回答
  •  無奈伤痛
    2020-12-05 00:55

    The compiler or interpreter will compute the step size (subtract 1 since it's zero based):

    f l n = [l !! i | i <- [n-1,n-1+n..]]
    

    The Haskell 98 Report: Arithmetic Sequences

提交回复
热议问题