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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:08:17+00:00 2026-06-16T19:08:17+00:00

I want to replace an element in a list with a new value only

  • 0

I want to replace an element in a list with a new value only at first time occurrence.
I wrote the code below but using it, all the matched elements will change.

replaceX :: [Int] -> Int -> Int -> [Int]
replaceX items old new = map check items where
check item  | item == old = new 
            | otherwise = item

How can I modify the code so that the changing only happen at first matched item?

Thanks for helping!

  • 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-16T19:08:19+00:00Added an answer on June 16, 2026 at 7:08 pm

    The point is that map and f (check in your example) only communicate regarding how to transform individual elements. They don’t communicate about how far down the list to transform elements: map always carries on all the way to the end.

    map :: (a -> b) -> [a] -> [b]
    map _ []     = []
    map f (x:xs) = f x : map f xs
    

    Let’s write a new version of map — I’ll call it mapOnce because I can’t think of a better name.

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

    There are two things to note about this type signature:

    1. Because we may stop applying f part-way down the list, the input list and the output list must have the same type. (With map, because the entire list will always be mapped, the type can change.)

    2. The type of f hasn’t changed to a -> a, but to a -> Maybe a.

      • Nothing will mean “leave this element unchanged, continue down the list”
      • Just y will mean “change this element, and leave the remaining elements unaltered”

    So:

    mapOnce _ []     = []
    mapOnce f (x:xs) = case f x of
            Nothing -> x : mapOnce f xs
            Just y  -> y : xs
    

    Your example is now:

    replaceX :: [Int] -> Int -> Int -> [Int]
    replaceX items old new = mapOnce check items where
        check item  | item == old = Just new 
                    | otherwise   = Nothing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to replace only the first matching element in a string instead of
I want to take an XML file and replace an element's value. For example
I want to replace a regex with '*', but only if the regex is
I want replace a(or each) element(number) instead another element(number) in a class while running
I want to replace duplicate elements from a vector with 0, and keep only
I have a list, with an li style defined. I want to replace the
I have an unordered list of 50 items. I want to show only 10
I have this deeply nested list (list of lists) and I want to replace
I want to refine the raw text by using regular expression, given a list
I am using ConcurrentSkipListSet, which I fill with 20 keys. I want to replace

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.