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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:45:58+00:00 2026-05-23T08:45:58+00:00

The following code is a DCG to replace all occurences of Find w/ Replace

  • 0

The following code is a DCG to replace all occurences of Find w/ Replace in Request & put the answer in Result. Thanks to mat, for the code, in this question.

eos([], []).

replace(_, _) --> call(eos), !.
replace(Find, Replace), Replace -->
        Find,
        !,
        replace(Find, Replace).
replace(Find, Replace), [C] -->
        [C],
        replace(Find, Replace).

substitute(Find, Replace, Request, Result):-
        phrase(replace(Find, Replace), Request, Result).

In SWI-Prolog, this expands to the following.

replace(_, _, A, B) :-
        call(eos, A, C), !,
        B=C.
replace(A, D, B, F) :-
        phrase(A, B, C), !,
        E=C,
        replace(A, D, E, G),
        phrase(D, F, G).
replace(B, C, A, E) :-
        A=[F|D],
        replace(B, C, D, G),
        E=[F|G].

substitute(A, B, C, D) :-
        phrase(replace(A, B), C, D).

eos([], []).

Is this code tail-recursive? There is a call to phrase after the recursive call to replace in the 2nd definition of the predicate replace. There’s also E=[F|G] after the recursive call to replace in the 3rd definition of replace. I thought that, if the recursive calls were made last, the code would be tail-recursive. If the generated code isn’t tail-recursive, is there any way to get Prolog to generate tail-recursive code? 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-05-23T08:45:59+00:00Added an answer on May 23, 2026 at 8:45 am

    Above code contains quite complex constructs like a very far reaching generalization of a semicontext. Please note that above, both Find and Replace can be general non-terminals – not only lists.

    So let’s consider a simpler case:

    iseq([]) --> [].
    iseq([E|Es]) --> iseq(Es), [E].
    

    which expands in many Prologs to:

    iseq([], Xs, Xs).
    iseq([E|Es], Xs0,Xs) :-
       iseq(Es, Xs0,Xs1),
       Xs1 = [E|Xs].
    

    This isn’t tail recursive either, but it can be made to be so by exchanging the two goals.
    Still, many consider above translation preferable, since it obviously retains certain desirable properties and also leads to a frequently more efficient execution.

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

Sidebar

Related Questions

The following code should find the appropriate project tag and remove it from the
following code is used to find url from a string with php. Here is
Following code: def __init__(self, url, **kwargs): for key in kwargs.keys(): url = url.replace('%%s%' %
Following code retrieves all the songs present in my iPhone but I want to
Following code: $this->addElement('text', 'email', array( 'label' => 'Your email address:', )); $this->addElement('submit', 'submit', array(
Following code has been picked up from this blog function! Privatize() let priorMethod =
The following code is intended to do a purely ajax POST request, instead it
Following code, credit: Guffa. Hello All, I'm trying to add controls to a Form
Following code is an example, I just want to know if this can be
following code works properly but it applies same cell text to all sections. How

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.