Article View: comp.lang.prolog
Article #2433Re: Efficiency and 'good' Prolog (was: Help !!)
From: Fergus James HENDERSON
Date: Thu, 28 Jan 1993 03:24
Date: Thu, 28 Jan 1993 03:24
48 lines
2561 bytes
2561 bytes
wunderwa@informatik.tu-muenchen.de (Jens Wunderwa) writes: >I have edited both max-versions for better comparability: > >max_heiner( [Max], Max ) :- !. >max_heiner( [MaxSoFar, First |Rest], Max ) :- > MaxSoFar > First, !, > max_heiner( [MaxSoFar |Rest], Max ). >max_heiner( [_|Rest], Max ) :- > max_heiner( Rest, Max ). > > >max_hamer( [First |Rest], Max ) :- > max_hamer( First, Rest, Max ). > >max_hamer( Max, [], Max ) :- !. >max_hamer( MaxSoFar, [First |Rest], Max ) :- > MaxSoFar > First, !, > max_hamer( MaxSoFar, Rest, Max ). >max_hamer( _, [First |Rest], Max ) :- > max_hamer( First, Rest, Max ). >uses a temporary variable for the greatest element seen so far >whereas Heiner uses the first element of the input list for that >purpose. > >I think it's bad style to misuse the input for constructing >the result instead of applying the standard accumulator pair technique. > >Heiner's solution needs heap space for storing the intermediate >result making it 30 % slower. Hamer's first post has expired here, but didn't it use one '->' instead of two cuts? Using arrow is much better style than using cuts, which are never justified for deterministic code like this. It is much more readable, and can also be more efficient. I think that this is a more important stylistic issue than the use of an accumulator. It's also not good style to use the same functor name for max_hamer/2 and max_hamer/3. I would much rather name the latter max_hamer_2/3, which gives the compiler a better chance of catching errors if I accidentally use the wrong number of arguments somewhere. -- Fergus Henderson fjh@munta.cs.mu.OZ.AU This .signature virus is a self-referential statement that is true - but you will only be able to consistently believe it if you copy it to your own .signature file!
Technical Details
Message-ID:
<9302814.20137@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!amdahl!rtech!sgiblab!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjhReferences:
<lenn.726916615@du9ds4> <j_hamer-210193105028@john-ha.cs.aukuni.ac.nz> <93021.154835U35395@uicvm.uic.edu> <1993Jan21.223806.19004@colorado.edu> <93022.184813U35395@uicvm.uic.edu> <WUNDERWA.93Jan26114355@gsradig1.informatik.tu-muenchen.de>Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.prolog Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!amdahl!rtech!sgiblab!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) Subject: Re: Efficiency and 'good' Prolog (was: Help !!) Message-ID: <9302814.20137@mulga.cs.mu.OZ.AU> Sender: news@cs.mu.OZ.AU Organization: Computer Science, University of Melbourne, Australia References: <lenn.726916615@du9ds4> <j_hamer-210193105028@john-ha.cs.aukuni.ac.nz> <93021.154835U35395@uicvm.uic.edu> <1993Jan21.223806.19004@colorado.edu> <93022.184813U35395@uicvm.uic.edu> <WUNDERWA.93Jan26114355@gsradig1.informatik.tu-muenchen.de> Date: Thu, 28 Jan 1993 03:24:39 GMT Lines: 48