Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Why does this recursion not work?
%to get the tail of the sequence

tail(sekv(D,T),T).

%find the last element:

last(nil,nil).

last(sekv(D,nil),D). %only one element left

%the recursion

last(sekv(D,R),R2):- last(tail(R,R2),R2).

Why does this recursion not work?





Report this thread to moderator Post Follow-up to this message
Old Post
Anders Lindén
04-18-05 01:56 AM


Re: Why does this recursion not work?
Dear Anders,

> %to get the tail of the sequence
>
> tail(sekv(D,T),T).
>
> %find the last element:
>
> last(nil,nil).
>
> last(sekv(D,nil),D). %only one element left
>
> %the recursion
>
> last(sekv(D,R),R2):- last(tail(R,R2),R2).
>
> Why does this recursion not work?
>
>

Look at the last clause of last/2. Translate it into your native tongue.
Does it say what you intend it to say?

Also note that 'tail(R,R2)' does not unify with 'nil' or 'sekv(A,B)'. So
it is going to fail. If you want to say last(Tail,Last), where Tail is
the tail of Sekv, I think you want this:

tail(Sekv,Tail),
last(Tail,Last).

In English, the tail of Sekv is Tail, and the last element of Tail is
Last.

Note also that the use of descriptive variable names helps readability.

Best wishes,

Bill

Report this thread to moderator Post Follow-up to this message
Old Post
Bill Spight
04-18-05 01:56 AM


Re: Why does this recursion not work?
Hi,
May be it does not work because there is no clause for
last(tail(Something, SomethingElse), SomethingElse)


"Anders Lindén" <xxxx@xxx.xx> wrote in message
news:42624b12$1@griseus.its.uu.se...
> %to get the tail of the sequence
>
> tail(sekv(D,T),T).
>
> %find the last element:
>
> last(nil,nil).
>
> last(sekv(D,nil),D). %only one element left
>
> %the recursion
>
> last(sekv(D,R),R2):- last(tail(R,R2),R2).
>
> Why does this recursion not work?
>
>
>
>


Report this thread to moderator Post Follow-up to this message
Old Post
George Sp
04-19-05 01:57 AM


Re: Why does this recursion not work?
Anders Lindén wrote:
> %to get the tail of the sequence
>
> tail(sekv(D,T),T).
>
> %find the last element:
>
> last(nil,nil).
>
> last(sekv(D,nil),D). %only one element left
>
> %the recursion
>
> last(sekv(D,R),R2):- last(tail(R,R2),R2).
>
> Why does this recursion not work?
>

To save ink, I will replace your 'sekv' functor with the single-letter
functor 'd'.

What is the "tail" and what is the "last elment" in each of these cases:

nil

d(X,Y)

d(a,d(X,Y))

d(a,d(X,d(Y,nil)))

Report this thread to moderator Post Follow-up to this message
Old Post
student
04-19-05 08:59 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:16 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.