🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #4190

Re: Definition of list_length/2

#4190
From: Lee Naish
Date: Wed, 14 Feb 1990 01:13
21 lines
1038 bytes
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
Technical Details
Message-ID: <3179@munnari.oz.au>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!munnari.oz.au!lee
References: <2733@swi.swi.psy.uva.nl>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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