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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:38:00+00:00 2026-06-11T21:38:00+00:00

I have come across a pattern where, I start with a seed value x

  • 0

I have come across a pattern where, I start with a seed value x and at each step generate a new seed value and a value to be output. My desired final result is a list of the output values. This can be represented by the following function:

my_iter :: (a -> (a, b)) -> a -> [b]
my_iter f x = y : my_iter f x'
   where (x',y) = f x

And a contrived example of using this would be generating the Fibonacci numbers:

fibs:: [Integer]
fibs = my_iter (\(a,b) -> let c = a+b in ((b, c), c)) (0,1)
-- [1, 2, 3, 5, 8...

My problem is that I have this feeling that there is very likely a more idiomatic way to do this kind of stuff. What are the idiomatic alternatives to my function?

The only ones I can think of right now involve iterate from the Prelude, but they have some shortcomings.

One way is to iterate first and map after

my_iter f x = map f2 $ iterate f1 x
    where f1 = fst . f
          f2 = snd . f

However, this can look ugly if there is no natural way to split f into the separate f1 and f2 functions. (In the contrived Fibonacci case this is easy to do, but there are some situations where the generated value is not an “independent” function of the seed so its not so simple to split things)

The other way is to tuple the “output” values together with the seeds, and use a separate step to separate them (kind of like the “Schwartzian transform” for sorting things):

my_iter f x = map snd . tail $ iterate (f.fst) (x, undefined)

But this seems wierd, since we have to remember to ignore the generated values in order to get to the seed (the (f.fst) bit) and add we need an “undefined” value for the first, dummy generated value.

  • 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-06-11T21:38:01+00:00Added an answer on June 11, 2026 at 9:38 pm

    As already noted, the function you want is unfoldr. As the name suggests, it’s the opposite of foldr, but it might be instructive to see exactly why that’s true. Here’s the type of foldr:

    (a -> b -> b) -> b -> [a] -> b
    

    The first two arguments are ways of obtaining something of type b, and correspond to the two data constructors for lists:

    []  :: [a]
    (:) :: a -> [a] -> [a]
    

    …where each occurrence of [a] is replaced by b. Noting that the [] case produces a b with no input, we can consolidate the two as a function taking Maybe (a, b) as input.

    (Maybe (a, b) -> b) -> ([a] -> b)
    

    The extra parentheses show that this is essentially a function that turns one kind of transformation into another.

    Now, simply reverse the direction of both transformations:

    (b -> Maybe (a, b)) -> (b -> [a])
    

    The result is exactly the type of unfoldr.

    The underlying idea this demonstrates can be applied similarly to other recursive data types, as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an API and have come across the following pattern: My API
I'm just new to the ipad development world, and have come across the Instapaper
I have come across the SPA design pattern and have a doubt whether this
I'm learning about DDD, and have come across the statement that value-objects should be
Recently I have come across a curious pattern in some code. We know that
Recently I have come across Command Pattern . In this pattern Client is responsible
I've been reading up on the Factory pattern, and have come across articles that
Lately I have come across Null Object design pattern and my colleagues say it
I have never come across this pattern of code right here. Would anyone care
I have come across an annoying problem. I have made a system and now

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.