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!
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
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
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
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.
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
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
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
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
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
"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")
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
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
"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")
"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 >
"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")
"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")
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.
"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")