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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:01:50+00:00 2026-05-25T17:01:50+00:00

I’m struggling with Haskell, and the idea of using recursion to iterate over things.

  • 0

I’m struggling with Haskell, and the idea of using recursion to iterate over things.

For instance, how would

// this might seem silly but I need to do it
list1 = empty list
list2 = list of numbers
for i from 0 to N // N being a positive integer
    for each number in list2
        if number == i, add to list1

translate into the ‘functional paradigm’? Any guidance would be appreciated.

  • 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-25T17:01:51+00:00Added an answer on May 25, 2026 at 5:01 pm

    Going step by step:

    list2 = list of numbers
    

    We’ll take this as a given, so list2 is still just a list of numbers.

    for i from 0 to N // N being a positive integer
    

    The correct way to do this in Haskell is generally with a list. Laziness means that the values will be computed only when used, so traversing a list from 0 to N ends up being the same thing as the loop you have here. So, just [0..n] will do the trick; we just need to figure out what to do with it.

    for each number in list2
    

    Given “for each” we can deduce that we’ll need to traverse the entirety of list2 here; what we do with it, we don’t know yet.

    if number == i, add to list1
    

    We’re building list1 as we go, so ideally we want that to be the final result of the expression. That also means that at each recursive step, we want the result to be the list1 we have “so far”. To do that, we’ll need to make sure we pass each step’s result along as we go.

    So, getting down to the meat of it:

    The filter function finds all the elements in a list matching some predicate; we’ll use filter (== i) list2 here to find what we’re after, then append that to the previous step’s result. So each step will look like this:

    step :: (Num a) => [a] -> a -> [a]
    step list1 i = list1 ++ filter (== i) list2
    

    That handles the inner loop. Stepping back outwards, we need to run this for each value i from the list [0..n], with the list1 value being passed along at each step. This is exactly what fold functions are for, and in this case step is exactly what we need for a left fold:

    list2 :: (Num a) => [a]
    list2 = -- whatever goes here...
    
    step :: (Num a) => [a] -> a -> [a]
    step list1 i = list1 ++ filter (== i) list2
    
    list1 :: (Num a) => a -> [a]
    list1 n = foldl step [] [0..n]
    

    If you’re wondering where the recursion is, both filter and foldl are doing that for us. As a rule of thumb, it’s usually better to avoid direct recursion when there are higher-level functions to do it for you.


    That said, the algorithm here is silly in multiple ways, but I didn’t want to get into that because it seemed like it would distract from your actual question more than it would help.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.