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 5966909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:52:00+00:00 2026-05-22T19:52:00+00:00

I find myself often using a pattern where I transform a list with a

  • 0

I find myself often using a pattern where I transform a list with a function that consumes 1..n elements from the list and produces some result out of those. E.g.

process :: [a] -> [b]
process [] = []
process xs = first : rest
    where (first, xs') = consume xs
          rest         = process xs'

Where the consume function consumes a variable number of items from the list and returns a result and the remaining list items. Can I use some standard higher order function here instead of the explicit recursion?

  • 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-22T19:52:01+00:00Added an answer on May 22, 2026 at 7:52 pm

    The function you need is similar to unfoldr. If you would bring your consume into the following form:

    consume' :: [a] -> Maybe (b,[a])
    

    Where consume' returns Nothing in case of an empty list, or Just ... otherwise. This is a small wrapper, that captures that pattern:

    wrap :: ([a] -> (b,[a])) -> [a] -> Maybe (b,[a])
    wrap f [] = Nothing
    wrap f xs = Just $ f xs
    

    Then you can use consume with unfoldr with the original definition of consume :: [a] -> (b,[a]) like this:

    unfoldr (wrap consume)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself using the current pattern quite often in my code nowadays var
I often find myself using lambdas as some sort of local functions to make
When using GTK# from C# on Mono, I often find myself copying out C#
I find myself using the 'pattern' below rather unsettlingly often, when I want to
I often find myself using Integers to represent values in different spaces. For example...
Although using SQL FOR XML EXPLICIT is cumbersome, I find myself using it often.
While using Vim (at home and at work), I often find myself doing similar
My PHP code is split between many files, and often I find myself using
I often find myself writing a property that is evaluated lazily. Something like: if
I often find myself wanting to collapse an n-dimensional matrix across one dimension using

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.