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

  • Home
  • SEARCH
  • 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 449121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:44:16+00:00 2026-05-12T21:44:16+00:00

I’m new in haskell and I’m looking for some standard functions to work with

  • 0

I’m new in haskell and I’m looking for some standard functions to work with lists by indexes.

My exact problem is that i want to remove 3 elements after every 5. If its not clear enough here is illustration:

OOOOOXXXOOOOOXXX...

I know how to write huge function with many parameters, but is there any clever way to do this?

  • 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-12T21:44:17+00:00Added an answer on May 12, 2026 at 9:44 pm

    Two completely different approaches

    1. You can use List.splitAt together with drop:

      import Data.List (splitAt)
      f :: [a] -> [a]
      f [] = []
      f xs = let (h, t) = splitAt 5 xs in h ++ f (drop 3 t)
      

      Now f [1..12] yields [1,2,3,4,5,9,10,11,12]. Note that this function can be expressed more elegantly using uncurry and Control.Arrow.second:

      import Data.List (splitAt)
      import Control.Arrow (second)
      f :: [a] -> [a]
      f [] = []
      f xs = uncurry (++) $ second (f . drop 3) $ splitAt 5 xs
      

      Since we’re using Control.Arrow anyway, we can opt to drop splitAt and instead call in the help of Control.Arrow.(&&&), combined with take:

      import Control.Arrow ((&&&))
      f :: [a] -> [a]
      f [] = []
      f xs = uncurry (++) $ (take 5 &&& (f . drop 8)) xs
      

      But now it’s clear that an even shorter solution is the following:

      f :: [a] -> [a] 
      f [] = []
      f xs = take 5 xs ++ (f . drop 8) xs
      

      As Chris Lutz notes, this solution can then be generalized as follows:

      nofm :: Int -> Int -> [a] -> [a]
      nofm _ _ [] = []
      nofm n m xs = take n xs ++ (nofm n m . drop m) xs
      

      Now nofm 5 8 yields the required function. Note that a solution with splitAt may still be more efficient!

    2. Apply some mathematics using map, snd, filter, mod and zip:

      f :: [a] -> [a]
      f = map snd . filter (\(i, _) -> i `mod` 8 < (5 :: Int)) . zip [0..]
      

      The idea here is that we pair each element in the list with its index, a natural number i. We then remove those elements for which i % 8 > 4. The general version of this solution is:

      nofm :: Int -> Int -> [a] -> [a]
      nofm n m = map snd . filter (\(i, _) -> i `mod` m < n) . zip [0..]
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.