Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!newsstand.cit.cornell.edu!news.kei.com!simtel!harbinger.cc.monash.edu.au!news.rmit.EDU.AU!news.unimelb.EDU.AU!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Subject: Re: 1 IS SLEEPLESS IN SEATTLE
Message-ID: <9532917.4926@mulga.cs.mu.OZ.AU>
Sender: news@cs.mu.OZ.AU (CS-Usenet)
Organization: Computer Science, University of Melbourne, Australia
References: <1995Nov22.114517.1@vax.sbu.ac.uk> <48vg45INNodq@bhars12c.bnr.co.uk> <1995Nov23.130606.1@vax.sbu.ac.uk>
Date: Sat, 25 Nov 1995 06:08:26 GMT
Lines: 36

zhuhail@vax.sbu.ac.uk writes:

>>>  Version A:
>>>
>>>twice([], S):- !, S = [].
>>>twice([H|T], [[H,H]|ST]):-
>>>	twice(T, ST).
>
>
>>   Version B:
>> 
>> twice( [], [] ).
>> twice( [H|T], [[H,H]|ST] ):-
>> 	twice( T, ST ).
>
>Do you agree that:
>   Version A can do just in one direction, [a,b] -> [[a,a],[b,b]];
>   version B can do in two directions, [a,b] <--> [[a,a],[b,b]];

Yes.

>   version A is faster and uses less space than version B ( for just
>   [a,b] -> [[a,a],[b,b]] ).

No.  All but the most totally brain-dead Prolog compilers will do
indexing on the top-most functor of the first argument, so version B
should be just as efficient as version A.  In fact, if anything
version B is likely to be a more efficient, since the compiler
will probably generate slightly better code for the first clause of B
than of A.  (Compiling with NU-Prolog verifies my intuition - in fact
NU-Prolog also happens to generate better code for the recursive call
in B than in A, saving one bytecode instruction per recursive call.)

--
Fergus Henderson             	WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au              	PGP: finger fjh@128.250.37.3
