Number of List Elements



(%i3) mylength(x) := if x = [] then 0 else mylength(rest(x)) + 1;
(%o3) mylength(x):=if x=[] then 0 else mylength(rest(x))+1

(%i4) mylenth([a,b,c]);
(%o4) 3

(%i5) mylenth([]);
(%o5) 0

Opps, mylength should check that the input is a list:

(%i6) mylenth(a+b+c);
rest: argument must be a non-atomic expression; found a

--Barton

maxima-bounces at math.utexas.edu wrote on 08/24/2010 02:55:53 PM:

> [image removed] 
> 
> Re: [Maxima] Number of List Elements
> 
> Conrad Schiff 
> 
> to:
> 
> maxima
> 
> 08/24/2010 02:56 PM
> 
> Sent by:
> 
> maxima-bounces at math.utexas.edu
> 
> Isn't always the case that one finds what one wants only after one 
> asks the question.  Anyway, length() does the trick, so let me amend
> my question to ask how would one write the equivalent of length() for 
oneself.
> 
> Again thanks in advance,
> Conrad
> 
> --- On Tue, 8/24/10, Conrad Schiff <gravmath at yahoo.com> wrote:
> 
> > From: Conrad Schiff <gravmath at yahoo.com>
> > Subject: Number of List Elements
> > To: maxima at math.utexas.edu
> > Date: Tuesday, August 24, 2010, 3:39 PM
> > Perhaps the function I am looking for
> > is to be found in the help files but, if so, it has so far
> > elluded me.  What I am trying to do is find out the
> > number of the elements in a list.  Specifically, I want
> > a function that returns the number of elements of a
> > list.  Its behavior would look like:
> > 
> > A : [1,2,3];
> > B : [ [a,b], c, d+e];
> > 
> > num_elem(A) -> 3
> > num_elem(B) -> 3
> > 
> > I would also be interested in seeing how one would program
> > such a function for oneself.
> > 
> > Thanks in advance,
> > Conrad
> > 
> > 
> > 
> >       
> > 
> 
> 
> 
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima