home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / prolog / 2467 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.4 KB  |  60 lines

  1. Newsgroups: comp.lang.prolog
  2. Path: sparky!uunet!charon.amdahl.com!amdahl!rtech!sgiblab!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: Efficiency and 'good' Prolog (was: Help !!)
  5. Message-ID: <9302814.20137@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. 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>
  9. Date: Thu, 28 Jan 1993 03:24:39 GMT
  10. Lines: 48
  11.  
  12. wunderwa@informatik.tu-muenchen.de (Jens Wunderwa) writes:
  13.  
  14. >I have edited both max-versions for better comparability:
  15. >
  16. >max_heiner( [Max], Max ) :- !.
  17. >max_heiner( [MaxSoFar, First |Rest], Max ) :-
  18. >    MaxSoFar > First, !, 
  19. >    max_heiner( [MaxSoFar |Rest], Max ).
  20. >max_heiner( [_|Rest], Max ) :-
  21. >    max_heiner( Rest, Max ).
  22. >
  23. >
  24. >max_hamer( [First |Rest], Max ) :-
  25. >    max_hamer( First, Rest, Max ).
  26. >
  27. >max_hamer( Max, [], Max ) :- !.
  28. >max_hamer( MaxSoFar, [First |Rest], Max ) :-
  29. >    MaxSoFar > First, !, 
  30. >    max_hamer( MaxSoFar, Rest, Max ).
  31. >max_hamer( _, [First |Rest], Max ) :-
  32. >    max_hamer( First, Rest, Max ).
  33.  
  34. >uses a temporary variable for the greatest element seen so far 
  35. >whereas Heiner uses the first element of the input list for that 
  36. >purpose. 
  37. >
  38. >I think it's bad style to misuse the input for constructing
  39. >the result instead of applying the standard accumulator pair technique.
  40. >
  41. >Heiner's solution needs heap space for storing the intermediate 
  42. >result making it 30 % slower.
  43.  
  44. Hamer's first post has expired here, but didn't it use one '->'
  45. instead of two cuts? Using arrow is much better style than using cuts,
  46. which are never justified for deterministic code like this. It is much
  47. more readable, and can also be more efficient. I think that this is a more
  48. important stylistic issue than the use of an accumulator.
  49.  
  50. It's also not good style to use the same functor name for max_hamer/2 and
  51. max_hamer/3. I would much rather name the latter max_hamer_2/3, which gives
  52. the compiler a better chance of catching errors if I accidentally use the
  53. wrong number of arguments somewhere.
  54.  
  55. -- 
  56. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  57. This .signature virus is a self-referential statement that is true - but 
  58. you will only be able to consistently believe it if you copy it to your own
  59. .signature file!
  60.