Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6624939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:42:49+00:00 2026-05-25T21:42:49+00:00

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

  • 0

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

Simple task – we have a list and want to leave only each nth element in that list.
What is the most idiomatic way to do it in haskell?

off the top of my head it is something like:

dr n [] = []
dr n (x : xs) = x : (dr n $ drop n xs)

but I have a strong feeling that I’m overcomplicating the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:42:50+00:00Added an answer on May 25, 2026 at 9:42 pm

    Your solution is fine, but here are three other solutions using functions from Haskell’s base library.

    dr1 m = concatMap (take 1) . iterate (drop m)
    

    Of coarse, this will never terminate (because iterate never terminates). So perhaps a better solution would be to use unfoldr:

    {-# LANGUAGE TupleSections #-}
    import Data.Maybe
    dr2 m = unfoldr ((\x-> fmap (,drop m x) (listToMaybe x)))
    

    The function you pass to an unfold can get a bit ugly if you don’t know GHC extensions and concepts such as functors, here’s that solution again without the fancy foot-work (untested):

    dr2 m = unfoldr ((\x -> case listToMaybe x of
                             Nothing -> Nothing
                             Just i  -> Just (i,drop m x)))
    

    If you don’t like unfolds then consider a zip and a filter:

    dr3 m = map snd . filter ((== 1) . fst) . zip (cycle [1..m])
    

    Review

    Understand all these solutions are slightly different. Learning why will make you a better Haskell progammer. dr1 uses iterate and will thus never terminate (perhaps this is ok for infinite lists, but probably not a good overall solution):

    > dr1 99 [1..400]
    [1,100,199,298,397^CInterrupted.
    

    The dr2 solution will show every mth value by skipping values in the unfold. The unfold passes both the value to be used for the next unfolding and the result of the current unfolding in a single tuple.

    > dr2 99 [1..400]
    [1,100,199,298,397]
    

    The dr3 solution is slightly longer but probably easier for a beginner to understand. First you tag every element in the list with a cycle of [1..n, 1..n, 1..n ...]. Second, you select only the numbers tagged with a 1, effectively skipping n-1 of the elements. Third you remove the tags.

    > dr3 99 [1..400]
    [1,100,199,298,397]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What are C macros useful for? Every few months I get an
Possible Duplicate: Get a list of dates between two dates This is my sql:
Possible Duplicate: Get full path of a file with FileUpload Control I want to
Possible Duplicate: Get class name from extended class Suppose I have the following: class
Possible Duplicate: Get top 10 products for every category I am looking for an
Possible Duplicate: nth-child for every two table rows I'm trying to work my way
Possible Duplicate: Can you get the user’s scroll position every time they scroll with
Possible Duplicate: every derived table must have its own alias i have two MYSQL
Possible Duplicate: list every font a user's browser can display I am developing a
Possible Duplicate: iOS: Get location update every n minutes How can I get location

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.