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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:53:44+00:00 2026-06-16T00:53:44+00:00

I’m learning tail recursion and I’m having some difficulties in determining whether my function

  • 0

I’m learning tail recursion and I’m having some difficulties in determining whether my function is tail recursive or not (mainly on functions which I use another function).

I’ve implemented the two following functions but I’m not so sure if they are tail recursive or not.

The first one is a function to concatenate two lists.

conca list [] = list
conca [] result = result
conca (h:t) result = conca (init (h:t)) ( last(h:t):result ) 

concatenate::[a]->[a]->[a]
concatenate list1 list2 = conca list1 list2

The computations in the function are processed before the recursive call, but its using last and init, which aren’t tail recursive ( I checked their definition in http://ww2.cs.mu.oz.au/172/Haskell/tourofprelude.html )

The second function is to remove the first occurrence of a given number in a given list.

invert [] result = result
invert (h:t) result = invert t (h:result)

remov n [] aux result = invert result []
remov n (h:t) aux result
        | aux==1 = remov n t 1 (h:result)
        | n==h = remov n t 1 (result)
        | otherwise = remov n t 0 (h:result)

remove n list = remov n list 0 []

The parameter aux (which can assume 0 or 1 as value) is being used to mark if the ocurrence has been removed or not.

In the remove function while the partial result is passed down through the recursive call the list is being inverted, upon the end the list is without the first ocurrence but upside-down, thus it have to be inverted to be returned as a result.

  • 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-16T00:53:45+00:00Added an answer on June 16, 2026 at 12:53 am
    conca (h:t) result = conca (init (h:t)) ( last(h:t):result ) 
    

    is a tail call, but last(h:t):result starts life as an unevaluated thunk, so it’s a (hand-wavy) bit like these nested function calls are on the stack still.

    conca pattern matches on its first argument so init will be evaluated in the recursive call.

    conca is non-strict in its second argument, so those thunks won’t get evaluated while applying the recursive call of conca.


    remov is tail recursive, yes, but….

    Use True and False instead of 0 and 1 to make your code clearer:

    remov n [] found result = invert result []
    remov n (h:t) found result
            | found = remov n t True (h:result)
            | n==h = remov n t True (result)
            | otherwise = remov n t False (h:result)
    
    remove n list = remov n list False []
    

    This would be better not passing around so much data, reducing the copying of n and using two functions instead of a single function that tests a boolean parameter:

    remove' n list = seek list [] where
       seek []     result = invert result []
       seek (h:t) result | h == n    = got t result
                         | otherwise = seek t (h:result)
       got []    result = invert result []
       got (h:t) result = got t (h:result)
    

    but got a result just calculates reverse result ++ a, so you could write

    remove'' n list = seek list [] where
       seek []     result = invert result []
       seek (h:t) result | h == n    = invert result [] ++ t
                         | otherwise = seek t (h:result)
    

    However, it all seems rather a lot of effort, and still traverses the list twice. Why not go for a non-tail call:

    removeFast n [] = []
    removeFast n (h:t) | h == n = t
                       | otherwise = h:removeFast n t
    

    This has the benefit of producing its first element straight away rather than running the whole list, and short cuts to return t without further computation once it’s found the element to remove. Try racing length (removeFast 1 [1..100000]) against length (remove 1 [1..100000]) (vary the number of zeros according to your processor speed).


    If you want to make a more efficient tail recursive conca, you could use the trick from remove:

    conc this result = prepend (invert this []) result
    
    prepend [] result = result
    prepend (h:t) result = prepend t (h:result)
    

    As before, you’re traversing this twice, once inverting it, the other prepending it, but that’s still a linear algorithm, and much better than using init and last for each element, which is quadratic.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.