From: AR
Subject: modifying length function
Date: 
Message-ID: <achufd$nfn$1@woodrow.ucdavis.edu>
Hi

how do i modify

(defun all-length (l)

 (cond
     ((null l) 0)

      (t ( + (cond ( (listp (car l)) (all-length(car l) ))
                      ( t 1)

             )

             (all-length(cdr l)) ))

 ); end cond1

); end all-length


so that it returns 1 when tested with (all-length '(nil) )? Right now it
returns 0.

Thanks!

From: pizza
Subject: Re: modifying length function
Date: 
Message-ID: <pan.2002.05.23.15.21.50.408116.3767@parseerror.com>
On Thu, 23 May 2002 01:24:58 -0400, AR wrote:

> Hi
> 
> how do i modify
> 
> (defun all-length (l)
> 
>  (cond
>      ((null l) 0)
> 
>       (t ( + (cond ( (listp (car l)) (all-length(car l) ))
>                       ( t 1)
> 
>              )
> 
>              (all-length(cdr l)) ))
> 
>  ); end cond1
> 
> ); end all-length
> 
> 
> so that it returns 1 when tested with (all-length '(nil) )? Right now it
> returns 0.
> 
> Thanks!

change the only 0 in the function to 1

love,

pizza

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com
From: Bruce Hoult
Subject: Re: modifying length function
Date: 
Message-ID: <bruce-E35F2E.17324323052002@copper.ipg.tsnz.net>
In article <············@woodrow.ucdavis.edu>,
 "AR" <········@yahoo.com> wrote:

> Hi
> 
> how do i modify
> 
> (defun all-length (l)
> 
>  (cond
>      ((null l) 0)
> 
>       (t ( + (cond ( (listp (car l)) (all-length(car l) ))
>                       ( t 1)
> 
>              )
> 
>              (all-length(cdr l)) ))
> 
>  ); end cond1
> 
> ); end all-length
> 
> 
> so that it returns 1 when tested with (all-length '(nil) )? Right now it
> returns 0.

Change listp to consp would be one way.  There are others -- it all 
depends on what you want this function to do in general.

-- Bruce
From: AR
Subject: Re: modifying length function
Date: 
Message-ID: <achv4e$nra$1@woodrow.ucdavis.edu>
Well, it should count the # of elements in the top level of  the list. Takes
a list and counts # of atoms that occur in list at all levels.

Sample usage:

(ALL-LENGTH '(NIL (NIL) ((NIL))))
3

(ALL-LENGTH '(A (B C) (D (E F))))
6





"Bruce Hoult" <·····@hoult.org> wrote in message
································@copper.ipg.tsnz.net...
> In article <············@woodrow.ucdavis.edu>,
>  "AR" <········@yahoo.com> wrote:
>
> > Hi
> >
> > how do i modify
> >
> > (defun all-length (l)
> >
> >  (cond
> >      ((null l) 0)
> >
> >       (t ( + (cond ( (listp (car l)) (all-length(car l) ))
> >                       ( t 1)
> >
> >              )
> >
> >              (all-length(cdr l)) ))
> >
> >  ); end cond1
> >
> > ); end all-length
> >
> >
> > so that it returns 1 when tested with (all-length '(nil) )? Right now it
> > returns 0.
>
> Change listp to consp would be one way.  There are others -- it all
> depends on what you want this function to do in general.
>
> -- Bruce
From: Barry Margolin
Subject: Re: modifying length function
Date: 
Message-ID: <L48H8.7$1o4.59@paloalto-snr1.gtei.net>
In article <············@woodrow.ucdavis.edu>, AR <········@yahoo.com> wrote:
>Well, it should count the # of elements in the top level of  the list. Takes
>a list and counts # of atoms that occur in list at all levels.

You just wrote two contradictory things.  Should it count element in the
top level (like the LENGTH function does?) or at all levels (i.e. the
number of leaves)?

Since later messages in the thread imply that it's all levels, how about:

(defun all-length (x)
  (if (atom x)
      1
      (reduce #'+ (mapcar #'all-length x))))

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Bruce Hoult
Subject: Re: modifying length function
Date: 
Message-ID: <bruce-6A3AA2.17445323052002@copper.ipg.tsnz.net>
In article <············@woodrow.ucdavis.edu>,
 "AR" <········@yahoo.com> wrote:

> Well, it should count the # of elements in the top level of  the list. Takes
> a list and counts # of atoms that occur in list at all levels.
> 
> Sample usage:
> 
> (ALL-LENGTH '(NIL (NIL) ((NIL))))
> 3
> 
> (ALL-LENGTH '(A (B C) (D (E F))))
> 6

Well that's what the change I mentioned does.

But it doesn't meet the english specification you just now gave.

What do you expect from this?

  (ALL-LENGTH '(() (()) ((()))))

-- Bruce
From: AR
Subject: Re: modifying length function
Date: 
Message-ID: <aci020$oqo$1@woodrow.ucdavis.edu>
I thought it should give 0 but the professor's sample output for
((ALL-LENGTH '(NIL (NIL) ((NIL)))) is  3

This is due in 1 hour and now I am freaking out!





"Bruce Hoult" <·····@hoult.org> wrote in message
································@copper.ipg.tsnz.net...
> In article <············@woodrow.ucdavis.edu>,
>  "AR" <········@yahoo.com> wrote:
>
> > Well, it should count the # of elements in the top level of  the list.
Takes
> > a list and counts # of atoms that occur in list at all levels.
> >
> > Sample usage:
> >
> > (ALL-LENGTH '(NIL (NIL) ((NIL))))
> > 3
> >
> > (ALL-LENGTH '(A (B C) (D (E F))))
> > 6
>
> Well that's what the change I mentioned does.
>
> But it doesn't meet the english specification you just now gave.
>
> What do you expect from this?
>
>   (ALL-LENGTH '(() (()) ((()))))
>
> -- Bruce
From: Bruce Hoult
Subject: Re: modifying length function
Date: 
Message-ID: <bruce-8316A2.18021423052002@copper.ipg.tsnz.net>
In article <············@woodrow.ucdavis.edu>,
 "AR" <········@yahoo.com> wrote:

> "Bruce Hoult" <·····@hoult.org> wrote in message
> ································@copper.ipg.tsnz.net...
> > In article <············@woodrow.ucdavis.edu>,
> >  "AR" <········@yahoo.com> wrote:
> >
> > > Well, it should count the # of elements in the top level of  the 
> > > list. Takes a list and counts # of atoms that occur in list at 
> > > all levels.
> > >
> > > Sample usage:
> > >
> > > (ALL-LENGTH '(NIL (NIL) ((NIL))))
> > > 3
> > >
> > > (ALL-LENGTH '(A (B C) (D (E F))))
> > > 6
> >
> > Well that's what the change I mentioned does.
> >
> > But it doesn't meet the english specification you just now gave.
> >
> > What do you expect from this?
> >
> >   (ALL-LENGTH '(() (()) ((()))))
> 
> I thought it should give 0 but the professor's sample output for
> ((ALL-LENGTH '(NIL (NIL) ((NIL)))) is  3

OK, good, so you do realize the two are the same.


The modification I previously suggested makes it pass all the tests 
you've posted here, plus the others you mailed to me privately

(ALL-LENGTH '(NIL (NIL) ((NIL))))
3

(ALL-LENGTH '(A (B C) (D (E F))))
6

(ALL-LENGTH '(NIL))
1

(ALL-LENGTH NIL)
0

What I'd do is show both versions -- the one that meets the English 
spec, and also the one that matches the examples -- and explain to the 
professor why they are an idiot, er, where their understanding of Lisp 
is lacking.

OK, perhaps that's being too harsh.  NIL *is* technically an atom.  But 
it seems a rather silly function if you want it to work that way.

-- Bruce
From: AR
Subject: Re: modifying length function
Date: 
Message-ID: <aci0u2$plg$1@woodrow.ucdavis.edu>
I change listp to consp in the true condition statement and it causes a
stack overflow error or something (using ACL to compile)
From: AR
Subject: Re: modifying length function
Date: 
Message-ID: <aci1rc$q08$1@woodrow.ucdavis.edu>
consp works on clisp!!

thank you!

"Bruce Hoult" <·····@hoult.org> wrote in message
································@copper.ipg.tsnz.net...
> In article <············@woodrow.ucdavis.edu>,
>  "AR" <········@yahoo.com> wrote:
>
> > "Bruce Hoult" <·····@hoult.org> wrote in message
> > ································@copper.ipg.tsnz.net...
> > > In article <············@woodrow.ucdavis.edu>,
> > >  "AR" <········@yahoo.com> wrote:
> > >
> > > > Well, it should count the # of elements in the top level of  the
> > > > list. Takes a list and counts # of atoms that occur in list at
> > > > all levels.
> > > >
> > > > Sample usage:
> > > >
> > > > (ALL-LENGTH '(NIL (NIL) ((NIL))))
> > > > 3
> > > >
> > > > (ALL-LENGTH '(A (B C) (D (E F))))
> > > > 6
> > >
> > > Well that's what the change I mentioned does.
> > >
> > > But it doesn't meet the english specification you just now gave.
> > >
> > > What do you expect from this?
> > >
> > >   (ALL-LENGTH '(() (()) ((()))))
> >
> > I thought it should give 0 but the professor's sample output for
> > ((ALL-LENGTH '(NIL (NIL) ((NIL)))) is  3
>
> OK, good, so you do realize the two are the same.
>
>
> The modification I previously suggested makes it pass all the tests
> you've posted here, plus the others you mailed to me privately
>
> (ALL-LENGTH '(NIL (NIL) ((NIL))))
> 3
>
> (ALL-LENGTH '(A (B C) (D (E F))))
> 6
>
> (ALL-LENGTH '(NIL))
> 1
>
> (ALL-LENGTH NIL)
> 0
>
> What I'd do is show both versions -- the one that meets the English
> spec, and also the one that matches the examples -- and explain to the
> professor why they are an idiot, er, where their understanding of Lisp
> is lacking.
>
> OK, perhaps that's being too harsh.  NIL *is* technically an atom.  But
> it seems a rather silly function if you want it to work that way.
>
> -- Bruce
From: Bruce Hoult
Subject: Re: modifying length function
Date: 
Message-ID: <bruce-89FF97.19104623052002@copper.ipg.tsnz.net>
In article <············@woodrow.ucdavis.edu>,
 "AR" <········@yahoo.com> wrote:

> consp works on clisp!!
> 
> thank you!

No worries mate.  Make sure you explain the what the difference between 
consp and listp is.

-- Bruce
From: Coby Beck
Subject: Re: modifying length function
Date: 
Message-ID: <lK0H8.124992$xS2.9942689@news1.calgary.shaw.ca>
"Bruce Hoult" <·····@hoult.org> wrote in message
································@copper.ipg.tsnz.net...
> In article <············@woodrow.ucdavis.edu>,
>  "AR" <········@yahoo.com> wrote:
>
> > "Bruce Hoult" <·····@hoult.org> wrote in message
> > ································@copper.ipg.tsnz.net...
> > > In article <············@woodrow.ucdavis.edu>,
> > >  "AR" <········@yahoo.com> wrote:
> > >
> > > > Well, it should count the # of elements in the top level of  the
> > > > list. Takes a list and counts # of atoms that occur in list at
> > > > all levels.
> > > >
> > > > Sample usage:
> > > >
> > > > (ALL-LENGTH '(NIL (NIL) ((NIL))))
> > > > 3
> > > >
> > > > (ALL-LENGTH '(A (B C) (D (E F))))
> > > > 6
> > >
> > > Well that's what the change I mentioned does.
> > >
> > > But it doesn't meet the english specification you just now gave.
> > >
> > > What do you expect from this?
> > >
> > >   (ALL-LENGTH '(() (()) ((()))))
> >
> > I thought it should give 0 but the professor's sample output for
> > ((ALL-LENGTH '(NIL (NIL) ((NIL)))) is  3
>
> OK, good, so you do realize the two are the same.
>
>
> The modification I previously suggested makes it pass all the tests
> you've posted here, plus the others you mailed to me privately
>
> (ALL-LENGTH '(NIL (NIL) ((NIL))))
> 3
>
> (ALL-LENGTH '(A (B C) (D (E F))))
> 6
>
> (ALL-LENGTH '(NIL))
> 1
>
> (ALL-LENGTH NIL)
> 0
>
> What I'd do is show both versions -- the one that meets the English
> spec, and also the one that matches the examples -- and explain to the
> professor why they are an idiot, er, where their understanding of Lisp
> is lacking.
>
> OK, perhaps that's being too harsh.  NIL *is* technically an atom.  But
> it seems a rather silly function if you want it to work that way.

I dunno, why?  (length '(() () ())) is three.  Looking at:
(ALL-LENGTH '(() (()) ((())))) you can just see the answer should be three.
It feels funny writing it, but it still makes sense.

Here's one answer:

(defun all-length (tree)
  (reduce #'+
          (mapcar #'(lambda (elt)
                      (if (atom elt)
                          1
                        (all-length elt)))
                  tree)))

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Bruce Hoult
Subject: Re: modifying length function
Date: 
Message-ID: <bruce-81768E.19431223052002@copper.ipg.tsnz.net>
In article <························@news1.calgary.shaw.ca>,
 "Coby Beck" <·····@mercury.bc.ca> wrote:

> > OK, perhaps that's being too harsh.  NIL *is* technically an atom.  But
> > it seems a rather silly function if you want it to work that way.
> 
> I dunno, why?  (length '(() () ())) is three.  Looking at:
> (ALL-LENGTH '(() (()) ((())))) you can just see the answer should be three.
> It feels funny writing it, but it still makes sense.

Because it seems as if what the english spec is getting at is something 
that would give the same results as (length (flatten l)).  Which would 
be zero for the above example.

OK, so what do you think you should get for this?

  (ALL-LENGTH '((NIL NIL NIL)))

Choices are 0, 1, or 3 :-)

I think it should be zero.  You appear to think it should be one.  And 
I'd bet the guy's professor expects it to be three.

-- Bruce
From: AR
Subject: Re: modifying length function
Date: 
Message-ID: <aci9d0$s9r$1@woodrow.ucdavis.edu>
It should be 3, they treat NIL as an atom

...don't ask why :=)



"Bruce Hoult" <·····@hoult.org> wrote in message
································@copper.ipg.tsnz.net...
> In article <························@news1.calgary.shaw.ca>,
>  "Coby Beck" <·····@mercury.bc.ca> wrote:
>
> > > OK, perhaps that's being too harsh.  NIL *is* technically an atom.
But
> > > it seems a rather silly function if you want it to work that way.
> >
> > I dunno, why?  (length '(() () ())) is three.  Looking at:
> > (ALL-LENGTH '(() (()) ((())))) you can just see the answer should be
three.
> > It feels funny writing it, but it still makes sense.
>
> Because it seems as if what the english spec is getting at is something
> that would give the same results as (length (flatten l)).  Which would
> be zero for the above example.
>
> OK, so what do you think you should get for this?
>
>   (ALL-LENGTH '((NIL NIL NIL)))
>
> Choices are 0, 1, or 3 :-)
>
> I think it should be zero.  You appear to think it should be one.  And
> I'd bet the guy's professor expects it to be three.
>
> -- Bruce
From: Coby Beck
Subject: Re: modifying length function
Date: 
Message-ID: <yn8H8.127103$GG6.10543983@news3.calgary.shaw.ca>
"AR" <········@yahoo.com> wrote in message
·················@woodrow.ucdavis.edu...
> It should be 3, they treat NIL as an atom
>
> ...don't ask why :=)
>

Here's a pretty good reason that your professor considers nil to be an atom:

CL-USER 1 > (atom nil)
T

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Wade Humeniuk
Subject: Re: modifying length function
Date: 
Message-ID: <acj7mg$2ca$1@news3.cadvision.com>
"Coby Beck" <·····@mercury.bc.ca> wrote in message
······························@news3.calgary.shaw.ca...
>
> "AR" <········@yahoo.com> wrote in message
> ·················@woodrow.ucdavis.edu...
> > It should be 3, they treat NIL as an atom
> >
> > ...don't ask why :=)
> >
>
> Here's a pretty good reason that your professor considers nil to be an atom:
>
> CL-USER 1 > (atom nil)
> T
>

Yes, but,

CL-USER 2 > (listp nil)
T

CL-USER 3 > (atom nil)
T

CL-USER 4 >
From: Coby Beck
Subject: Re: modifying length function
Date: 
Message-ID: <E6aH8.37665$Ka.2794146@news2.calgary.shaw.ca>
"Wade Humeniuk" <········@cadvision.com> wrote in message
> "Coby Beck" <·····@mercury.bc.ca> wrote in message
> > "AR" <········@yahoo.com> wrote in message
> > > It should be 3, they treat NIL as an atom
> > >
> > > ...don't ask why :=)
> > >
> >
> > Here's a pretty good reason that your professor considers nil to be an
atom:
> >
> > CL-USER 1 > (atom nil)
> > T
>
> Yes, but,
>
> CL-USER 2 > (listp nil)
> T
>
> CL-USER 3 > (atom nil)
> T

And yet....

CL-USER 4 > (consp nil)
NIL

Kind of like a photon.. a particle and a wave, no mass, yet it can push
things...one of life's mystical realities.  :)

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Coby Beck
Subject: Re: modifying length function
Date: 
Message-ID: <Ql8H8.127101$GG6.10543764@news3.calgary.shaw.ca>
"Bruce Hoult" <·····@hoult.org> wrote in message
································@copper.ipg.tsnz.net...
> In article <························@news1.calgary.shaw.ca>,
>  "Coby Beck" <·····@mercury.bc.ca> wrote:
>
> > > OK, perhaps that's being too harsh.  NIL *is* technically an atom.
But
> > > it seems a rather silly function if you want it to work that way.
> >
> > I dunno, why?  (length '(() () ())) is three.  Looking at:
> > (ALL-LENGTH '(() (()) ((())))) you can just see the answer should be
three.
> > It feels funny writing it, but it still makes sense.
>
> Because it seems as if what the english spec is getting at is something
> that would give the same results as (length (flatten l)).  Which would
> be zero for the above example.

Are you sure?  nil is a very reasonable list element.  I think
(flatten '(() (()) ((())))) should return (() () ()) shouldn't it?  Maybe
I'll write flatten again and see if it does...

>
> OK, so what do you think you should get for this?
>
>   (ALL-LENGTH '((NIL NIL NIL)))
>
> Choices are 0, 1, or 3 :-)
>
> I think it should be zero.  You appear to think it should be one.  And
> I'd bet the guy's professor expects it to be three.

I would expect three.  I think my code would return that, but I havn't
tested.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Kaz Kylheku
Subject: Re: modifying length function
Date: 
Message-ID: <slrnaeqeso.vaa.kaz@localhost.localdomain>
On Thu, 23 May 2002 19:43:12 +1200, Bruce Hoult <·····@hoult.org> wrote:
>In article <························@news1.calgary.shaw.ca>,
> "Coby Beck" <·····@mercury.bc.ca> wrote:
>
>> > OK, perhaps that's being too harsh.  NIL *is* technically an atom.  But
>> > it seems a rather silly function if you want it to work that way.
>> 
>> I dunno, why?  (length '(() () ())) is three.  Looking at:
>> (ALL-LENGTH '(() (()) ((())))) you can just see the answer should be three.
>> It feels funny writing it, but it still makes sense.
>
>Because it seems as if what the english spec is getting at is something 
>that would give the same results as (length (flatten l)).  Which would 
>be zero for the above example.
>
>OK, so what do you think you should get for this?
>
>  (ALL-LENGTH '((NIL NIL NIL)))
>
>Choices are 0, 1, or 3 :-)

There is one more choice.  Since length returns the number of conses in a list,
one way to extend the semantics to trees is to continue counting conses.  Thus,
the answer 4 makes sense as well. The (NIL NIL NIL) list is an element, so it
gets counted, and then its three elements are counted.
From: Coby Beck
Subject: Re: modifying length function
Date: 
Message-ID: <ITaH8.127486$GG6.10580031@news3.calgary.shaw.ca>
"Kaz Kylheku" <···@localhost.localdomain> wrote in message
·······················@localhost.localdomain...
> On Thu, 23 May 2002 19:43:12 +1200, Bruce Hoult <·····@hoult.org> wrote:
> >In article <························@news1.calgary.shaw.ca>,
> > "Coby Beck" <·····@mercury.bc.ca> wrote:
> >
> >> > OK, perhaps that's being too harsh.  NIL *is* technically an atom.
But
> >> > it seems a rather silly function if you want it to work that way.
> >>
> >> I dunno, why?  (length '(() () ())) is three.  Looking at:
> >> (ALL-LENGTH '(() (()) ((())))) you can just see the answer should be
three.
> >> It feels funny writing it, but it still makes sense.
> >
> >Because it seems as if what the english spec is getting at is something
> >that would give the same results as (length (flatten l)).  Which would
> >be zero for the above example.
> >
> >OK, so what do you think you should get for this?
> >
> >  (ALL-LENGTH '((NIL NIL NIL)))
> >
> >Choices are 0, 1, or 3 :-)
>
> There is one more choice.  Since length returns the number of conses in a
list,
> one way to extend the semantics to trees is to continue counting conses.
Thus,
> the answer 4 makes sense as well. The (NIL NIL NIL) list is an element, so
it
> gets counted, and then its three elements are counted.

This is what the answer should be!  In fact that is what the first solution
I wrote actually did and it seemed to take some extra mental gymnastics to
get it to match the examples provided.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")