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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:12:14+00:00 2026-06-18T14:12:14+00:00

I am new to Haskell. I am trying to write a function that, given

  • 0

I am new to Haskell. I am trying to write a function that, given the list l, element x that exists in the list, and an element to insert y: insert element y before the first occurrence of the element x, in list l. If element x does not exist in the list, then leave the list unchanged.

I am having a lot of trouble with this one and would appreciate any suggestions.

This is what I tried (‘n’ is the first occurrence of element x):

insertSpecial :: Eq a => a -> a -> [a] -> [a]
insertSpecial let (ys,zs) = splitAt n xs in ys ++ [y] ++ zs
  • 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-18T14:12:15+00:00Added an answer on June 18, 2026 at 2:12 pm

    How about

    insertSpecial :: Eq a => a -> a -> [a] -> [a]
    insertSpecial x y []              = []
    insertSpecial x y (a:as) | a == x = y:a:as
    insertSpecial x y (a:as)          = a : insertSpecial x y as
    

    This approach uses a general method known as recursion. This means that the task at hand is broken down into one or several base case(s), for which the result can be defined easily, and a general case which can be solved by breaking down the work into parts. In the general case the function is then called recursively on the smaller task. The goal is to eventually end up in a base case at which point the work is done. See also this part from Learn you a Haskell.

    For insertSpecial we can define two base cases:

    1. If the list is empty it doesn’t contain the element we’re looking for and we want to leave the list unchanged in this case, so we simply return an empty list. We’re done.

    2. If the list is not empty and the first element is the one we are looking for we stick y in front of this list and return that. Again we’re done.

    That leaves us with the case where the list is not empty but the first element is not the one we are looking for. In this case we break up the work (the list) in two parts: the first element and the rest of the elements. We put the first element in front of the list that is returned by calling insertSpecial on the rest of the list. This is where the recursion happens: a call of insertSpecial calls itself.

    One thing that is maybe a little difficult to understand about this is how the last case produces a list that only differs from the original list by inserting an element at the right place. Let’s consider an example. Say we have the list

    ['h','e','l','o']
    

    Note that in Haskell, regular strings are just lists of characters so ['h','e','l','o'] == "helo". Also note that ['h','e','l','o'] is really syntactic sugar for 'h':'e':'l':'o':[]. (: takes an element and a list and prepends that element to the list. it is also called cons).

    Now let’s insert the missing 'l' in front of the 'o':

     insertSpecial 'o' 'l' "helo"
    

    Since "helo" is not empty 'h' is bound to a and "elo" is bound to as (furthermore, 'o' is bound to x and 'l' is bound to 'y'). Since a == 'h' /= 'o' == x we are in the third case, so

      insertSpecial 'o' 'l' ('h':"elo") = 'h' : insertSpecial 'o' 'l' "elo"
    

    Now with insertSpecial 'o' 'l' "elo" we again fall into the third case ("elo" is not empty and 'o' /= 'e'):

     insertSpecial 'o' 'l' ('e':"lo") = 'e' : insertSpecial 'o' 'l' "lo"
    

    Which again leads to the third case:

     insertSpecial 'o' 'l' ('l':"o") = 'l' : insertSpecial 'o' 'l' "o"
    

    Now in the last call we’re actually binding 'o' to a which is equal to x and we land in the second case, where we prepend the value of y ('l') to a ('o') and as ([]), so we get:

    insertSpecial 'o' 'l' ('o':[]) | 'o' == 'o' = 'l' : 'o' : []
    

    Putting all these substitutions together we get:

    insertSpecial 'o' 'l' "helo" = 
    'h' : insertSpecial 'o' 'l' "elo" =
    'h' : 'e' : insertSpecial 'o' 'l' "lo" = 
    'h' : 'e' : 'l' : insertSpecial 'o' 'l' "o" =
    'h' : 'e' : 'l' : 'l' : 'o' : []
    

    And as described above 'h' : 'e' : 'l' : 'l' : 'o' : [] == ['h','e','l','l','o'] == "hello". Yay.

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

Sidebar

Related Questions

I am new to Haskell. I am trying to write a program which given
I'm trying to write a Haskell function that takes makes use of this function:
I am trying to write a list function that takes a simple list and
I'm new to haskell and I'm trying to make a function that will multiply
I'm trying to write a function searching for a given element in a rose
I'm new to haskell guys. I'm trying to write a gcd executable file. ghc
New to Haskell and have a stumbling block. I'm trying to filter a list
I am new to Haskell programming, Foreign Function Interface and Stackoverflow. I am trying
I'm really new to Haskell and I'm stuck on trying to map the first
I am new to Haskell and trying to fiddle with some test cases 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.