Path: utzoo!attcan!uunet!munnari.oz.au!lee From: lee@munnari.oz.au (Lee Naish) Newsgroups: comp.lang.prolog Subject: Re: Definition of list_length/2 Message-ID: <3179@munnari.oz.au> Date: 14 Feb 90 01:13:43 GMT References: <2733@swi.swi.psy.uva.nl> Sender: news@cs.mu.oz.au Reply-To: lee@munmurra.UUCP (Lee Naish) Organization: Comp Sci, University of Melbourne Lines: 21 In article <2733@swi.swi.psy.uva.nl> anjo@swi.psy.uva.nl () writes: >Below is a definition of list_length/2 (length/2 in Edinburgh >compatibles). I would like to know whether there is a better/shorter >solution >list_length_non([], 0). >list_length_non([_|Xs], N) :- > list_length_non(Xs, M), > N is M+1. This bit is not tail recursive and can easily be improved using an accumulator (this is something you should always look out for): list_length_non(Xs, N) :- list_length_non_ac(Xs, 0, N). list_length_non_ac([], N, N). list_length_non_ac([_|Xs], N0, N) :- N1 is N0 + 1, list_length_non_ac(Xs, N1, N). lee