Subject: Re: http://www.swi.psy.uva.nl/projects/SWI-Prolog/recursive or not

From: Elias <[email-masked]>
Date: Sun, 2 Apr 2000 20:19:24 +0100 (GMT)

Prolog is not a recursive programming language. "Pure prolog programs are
logic programs." -- Art of Prolog

Like most of the other programming languages, Prolog supports recursion
and most Prolog programs depend a lot on this.

A simple and commonly used Prolog recursion is:

append([],L,L).
append([H|T],L,[H|R]) :-
	append(T,L,R).

This is already in the SWI-Prolog library. It is used to concatenate two
lists. For example, "append([biscuits,chocolate],[fish,chips],X)." would
cause X to be instantiated to "[biscuits,chocolate,fish,chips]"

The first line of the code is the stopping condition, which stops the
recursion from going on forever. It basically means if the first argument
is an empty list, then return the second list. Note that the third
argument is what it should return given the first two.

The second clause recursively calls append to concatenate the tail of the
first list with the second list returning the result in R. Then, R is
returned as the tail of a list which has the head of the first list.

Books that introduce Prolog should have something about append. Ivan
Bratko's "Prolog: Programming for Artificial Intelligence" calls it
"conc"

Elias

On Sun, 2 Apr 2000 [email-masked] wrote:

> I find it a bit difficult to understand wheather Proog is recursive or not.
> Sorry, that was Prolog. I am currently learning SWI-Prolog on unix and 
> understanding what  recursive is meant to be in the prolog sense 
> is a bit complicated. Could someone give an example of a recursive
> program using lists with an explanation.
> 
> 
> ----------------
> * To UNSUBSCRIBE, please use the HTML form at
> 
>     http://www.swi.psy.uva.nl/projects/SWI-Prolog/index.html#mailinglist
> 
> or send mail to [email-masked] using the Subject: "unsubscribe"
> (without the quotes) and *no* message body.
> 
> ** An ARCHIVE of this list is maintained at
> 
>     http://www.swi.psy.uva.nl/projects/SWI-Prolog/mailinglist/archive/
>