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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:31:57+00:00 2026-06-12T10:31:57+00:00

I have a function that takes in a list, and if there are 2

  • 0

I have a function that takes in a list, and if there are 2 identical and successive numbers in the list, and if there is a number, x, elsewhere in the list, that is equivalent, then I want to change x to 0 and return the list.

twoAdjThenThirdZero (x:y:xs) = [if x == y && x `elem` xs then 0 else x | x <- xs]

For some reason, it is omitting the first two elements in the list every time I try to run it.

*Main> twoAdjThenThirdZero [2,3,4,1,2,0,2,3,3]

[4,1,2,0,2,0,0]

Also, the above case is doing the opposite of what I would like. I want to keep the two 3’s at the end of the list and make the second element, that 3, to be 0. But it was switched around.

*Main> twoAdjThenThirdZero [2,2,3,1,2,4]

[3,1,0,4]

Does anyone know why this is? Thanks in advance!

  • 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-12T10:31:58+00:00Added an answer on June 12, 2026 at 10:31 am

    I see multiple problems here. You start by destructuring the parameter list in the function declaration twoAdjThenThirdZero (x:y:xs). If you want to continue to get x and y for each step, you have to recurse. Instead you switch to using a list comprehension, and a duplicate of x. In the list comprehension you go through xs, which is all elements of the function parameter except the first two (x and y).

    If you read the list comprehension out loud I think you can figure it out.

    “if x equals y and x is an element of xs then zero else x, for every x in xs”. But you want it done for every x in x+y+xs! You are also using the name “x” in two ways, both in your destructuring of the function arguments and as a variable in the list comprehension.

    EDIT:

    Now I see what you mean. You just have to add that explicit recursion to what you have already.

    twoAdjThenThirdZero [] = []
    twoAdjThenThirdZero [x] = [x]
    twoAdjThenThirdZero (x:y:xs) 
      | x == y && x `elem` xs = x : y : twoAdjThenThirdZero [if z == x then 0 else z | z <- xs]
      | otherwise             = x : twoAdjThenThirdZero (y:xs)
    

    I hope that makes sense to you, if it doesn’t, I’ll try to explain it further!

    EDIT:

    phynfo has posted a slightly simpler version of what I was writing!

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

Sidebar

Related Questions

I have a very simple function that takes a list of comma separated (x,y)
I have a function that takes an input string and then runs the string
I have a function that takes a string-like argument. I want to decide if
I have a function that takes a number such as 36, and reverses it
Let's say I have a function that takes a list of function-list pairs, and
I have a logging function that takes in a variable number of arguments and
I have a JavaScript function that takes two required parameters and then arbitrarily many
I have a function that takes a list that either has two or three
Say I have a function that takes a list and does something: (defun foo(aList)
I have a function that takes a By element, By by = By.xpath(xpathString). Inside

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.