From etawil@essex.ac.uk  Sun Apr  2 21:19:29 2000
Received: from seralph10.essex.ac.uk (seralph10.essex.ac.uk [155.245.240.160])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id VAA14836
	for <prolog@swi.psy.uva.nl>; Sun, 2 Apr 2000 21:19:29 +0200 (MET DST)
Received: from sunlab1.essex.ac.uk
	([155.245.160.1] helo=sunlab1 ident=etawil)
	by seralph10.essex.ac.uk with esmtp (Exim 3.13 #1)
	id 12bpuB-0003VD-00; Sun, 02 Apr 2000 20:19:27 +0100
Date: Sun, 2 Apr 2000 20:19:24 +0100 (GMT)
From: Elias <etawil@essex.ac.uk>
X-Sender: etawil@sunlab1
To: s5008211@manu.usp.ac.fj
cc: prolog@swi.psy.uva.nl
Subject: Re: http://www.swi.psy.uva.nl/projects/SWI-Prolog/recursive or not
In-Reply-To: <3ULmSB.A.yCB.k4q54@swi>
Message-ID: <Pine.LNX.4.05.10004021957130.7663-100000@sunlab1>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII

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 s5008211@manu.usp.ac.fj 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 prolog-request@swi.psy.uva.nl 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/
> 

