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

  • Home
  • SEARCH
  • 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 8791181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:48:28+00:00 2026-06-13T22:48:28+00:00

How can I do a function (recursivily) that substitute the argument b(1) for an

  • 0

How can I do a function (recursivily) that substitute the argument b(1) for an argument a(0) on a list?
For example:

    substitute 0 1 [1,0,3,0,4,0,0]
    [1,1,3,1,4,1,1]

Thanks.

  • 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-13T22:48:29+00:00Added an answer on June 13, 2026 at 10:48 pm

    The simplest is to use list comprehension:

    subst a b xs = [c | x<-xs, let c=if x==a then b else x].

    But that’s no recursion. With recursion, it is just a case analysis for list structure (i.e. structural recursion):

    subst a b [] = []
    subst a b (x:xs) 
       | x==a      = b:subst a b xs
       | otherwise = x:subst a b xs
    

    which is an instance of the foldr (or map) pattern:

    subst a b = foldr (\x xs-> (if x==a then b else x) : xs) [] 
    subst a b = map   (\x   ->  if x==a then b else x      ) 
    

    It is usually advisable to use a “worker” function in recursive definitions, like so:

    subst a b xs = go xs
      where
        go [] = []
        go (x:xs) 
           | x==a      = b:go xs
           | otherwise = x:go xs
    

    but if you stare at it for a moment you recognize that it is follows the map pattern. In Haskell recursive patterns are captured by higher-order functions, like map, filter etc.

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

Sidebar

Related Questions

Can function argument have hint in cfscript (CF9)? CFML style: <cffunction name=myFunc output=false returntype=void>
It seems that JavaScript's Date() function can only return local date and time. Is
I'm trying to write a simple recursive function that look over list and return
Example: A function that takes a function (that takes a function (that ...) and
can nvl() function be cascaded,...it was asked me in IBM interview .....and why????
Can a function in R return not one, but two vectors? (I am really
Can a function return more than one value directly (i.e., without returning in parameters
Which function can I use to get the content of a plain text file?
I can create function pointers in Fortran 90, with code like real, external ::
How can eregi() function can be replaced here with preg_match() function in below given

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.