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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:34:01+00:00 2026-06-15T07:34:01+00:00

Recently I am trying to solve a problem using Foldr. The task is following:

  • 0

Recently I am trying to solve a problem using Foldr. The task is following:

In:[5,1,3,8,2,4,7,1]
Out:[16,8]

It means, I will double those element of the input list which is in the odd index position and even digit. I wrote the program without using foldr which is following:(It shows pattern match failure: head[])

findPos list elt =
  map fst $ filter ((elt==).snd) $ zip [0..] list

doublePos [] = []
doublePos (x:xs)
  | ((head(findPos xs x)`mod` 2) /= 0) && (x `mod` 2 == 0) =
      [2*x] ++ doublePos xs
  | otherwise = doublePos xs

How do I write this program using foldr?

  • 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-15T07:34:03+00:00Added an answer on June 15, 2026 at 7:34 am

    foldr isn’t really a good choice for this function, as you need to pass the parity of the index of each element from the front of the list.

    A list comprehension is probably the cleanest:

    doublePos xs = [2*x | (i,x) <- zip [0..] xs, even x, odd i] 
    

    or you could use plain old recursion:

    doublePos' (_:x:xs)
      | even x    = (2*x) : doublePos' xs
      | otherwise =         doublePos' xs
    doublePos' _ = []
    

    Though, if you must use foldr, you can do it by having the accumulator be a function
    which takes the parity of the current index as an argument:

    doublePos'' xs = foldr step (const []) xs False where
      step x next p
        | p && even x = 2*x : next (not p)
        | otherwise   = next (not p)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I was trying to solve a small AI problem but got stuck in
I was recently trying to solve some task in Python and I have found
I'm trying to solve a two-dimensional random walk problem from the book, exploring python.
Recently, I'm trying to solve all the exercises in CLRS. but there are some
Hello I am trying to solve this problem for past 5 hours now my
I'm trying to build a program to solve a problem in a text book
I just recently started trying out T4MVC and I like the idea of eliminating
I was trying to solve this issue recently, but i don't really know how.
I've recently been trying to figure out how to get PyCrypto to recognize PEM's
I have recently come across a problem that I cannot seem to solve. I

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.