From: CH
Subject: lisp code
Date: 
Message-ID: <353E9580.86D65E40@quadrant.net>
Hi, I am trying to write a lisp code which counts atoms within a list
Can this be done only with lisp primitives? Thanks.
--CH

From: Barry Margolin
Subject: Re: lisp code
Date: 
Message-ID: <sWw%.26$Qv4.583053@cam-news-reader1.bbnplanet.com>
In article <·················@quadrant.net>, CH  <······@quadrant.net> wrote:
>Hi, I am trying to write a lisp code which counts atoms within a list
>Can this be done only with lisp primitives? Thanks.

Yes.  I'll bet if you were a professor you might even assign this as an
exercise to beginning Lisp programming students.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
From: CH
Subject: Re: lisp code
Date: 
Message-ID: <353EDFC4.D6D1088A@quadrant.net>
Perhaps. However, I am just learning Common lisp and lisp primitives is about as
much as I know :-)  (yes, this makes me a newbie...  :-)    )
I know about ... quote, car, cdr, cons, equal, atom, cond, lambda.
Can you help ? Thanks.

--CH


Barry Margolin wrote:

> In article <·················@quadrant.net>, CH  <······@quadrant.net> wrote:
> >Hi, I am trying to write a lisp code which counts atoms within a list
> >Can this be done only with lisp primitives? Thanks.
>
> Yes.  I'll bet if you were a professor you might even assign this as an
> exercise to beginning Lisp programming students.
>
> --
> Barry Margolin, ······@bbnplanet.com
> GTE Internetworking, Powered by BBN, Cambridge, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
From: Kent M Pitman
Subject: Re: lisp code
Date: 
Message-ID: <sfw4szjkxrn.fsf@world.std.com>
CH <······@quadrant.net> writes:

> Perhaps. However, I am just learning Common lisp and lisp primitives is about as
> much as I know :-)  (yes, this makes me a newbie...  :-)    )
> I know about ... quote, car, cdr, cons, equal, atom, cond, lambda.
> Can you help ? Thanks.

The term "primitive" is a subjective term that's not part of the language definition.
For example, of >, >=, <, <=, =, and /= you could imagine that <, =, and NOT was
sufficient to implement the rest. So are >, =, and NOT.  So which are primitive?
Depends on your instructor or philosophical mentor.

Some consider IF primitive and COND derived, some the other way around.

Some consider "macros" non-primitive even though they come with the language and
in some implementations are provided native support (i.e., are understood directly
without macro expansion by the interpreter and/or compiler).

There are 978 symbols in Common Lisp.  Every one is just as omnipresent as every
other in a conforming implementation.  I consider them all "primitive" in some ways.
To browse them, see the symbol index in the Common Lisp HyperSpec(TM) at
 http://www.harlequin.com/education/books/HyperSpec/FrontMatter/Symbol-Index.html
To find out about downloading your own local copy of the HyperSpec (about 16MB),
see
 http://www.harlequin.com/education/books/HyperSpec/
From: Jeff Dalton
Subject: Re: lisp code
Date: 
Message-ID: <x23eew1r3v.fsf@gairsay.aiai.ed.ac.uk>
Kent M Pitman <······@world.std.com> writes:

> The term "primitive" is a subjective term that's not part of the
> language definition.

In course, students are often told about some small number of Lisp
primitives (the traditional set is something like car, cdr, cons,
atom, and eq) that suffice for writing "any" function.  (Of course,
you need some others, e.g. cond or if.)

Perhaps something like that is involved somewhere here.

-- jeff
From: Kent M Pitman
Subject: Re: lisp code
Date: 
Message-ID: <sfwlnsofmbk.fsf@world.std.com>
Jeff Dalton <····@gairsay.aiai.ed.ac.uk> writes:

> In course, students are often told about some small number of Lisp
> primitives (the traditional set is something like car, cdr, cons,
> atom, and eq) that suffice for writing "any" function. 

Yeah, I'm familiar with that point but was making a counter-point that
in the external world, primitiveness is relative in that the set of 
primitive things is arbitrarily chosen by each instructor and not
uniquely determined.

Further, btw, the set that's chosen is invariably (in my experience)
insufficient for writing "any" function unless you mean "any" in the
completely boring Turing-powerful sense.

For example, try as you might, car, cdr, cons, and atom can't open a
network connection, access a database, bind a special variable, spawn
a multitasked thread, signal an abort interrupt, listen to see if a
stream has characters waiting on it, map over a hash table, or change
the color of my screen.  In truth, more primitives are secretly required.
Confirming again that what's primitive is just subjective.

But at the very least, people who are going to ask the Internet to do
their homework should come quickly to grips with the fact that the
word "primitive" is largely meaningless out of the context of a
specific classroom.
From: William Paul Vrotney
Subject: Re: lisp code
Date: 
Message-ID: <vrotneyEruI1A.9Hz@netcom.com>
In article <·················@quadrant.net> CH <······@quadrant.net> writes:
> 
> Hi, I am trying to write a lisp code which counts atoms within a list
> Can this be done only with lisp primitives? Thanks.
> --CH
> 

How primitive?  This can actually be done using *only* atoms and lists!

Assuming the exercise is not to count atoms in a recursive list:

(defun make-absolutely-sure-its-an-atom (obj)
  (not (consp obj)))

(defun make-sure-its-an-atom (obj)
  (unless nil (make-absolutely-sure-its-an-atom obj)))

(defun number-of-atoms (list)
  (let ((number 0))
    (dolist (x list number)
      (if (atom x)
        (if (make-sure-its-an-atom x) (incf number))))))

(number-of-atoms '(1 2 3 ())) => 4
(number-of-atoms '(1 2 3 (4))) => 3



Sorry, absolutely couldn't resist! :-)

-- 

William P. Vrotney - ·······@netcom.com
From: Sunil Mishra
Subject: Re: lisp code
Date: 
Message-ID: <efywwcg7768.fsf@clairmont.cc.gatech.edu>
In article <·················@netcom.com> ·······@netcom.com (William Paul Vrotney) writes:

   In article <·················@quadrant.net> CH <······@quadrant.net> writes:
   > 
   > Hi, I am trying to write a lisp code which counts atoms within a list
   > Can this be done only with lisp primitives? Thanks.
   > --CH
   > 

   How primitive?  This can actually be done using *only* atoms and lists!

   Assuming the exercise is not to count atoms in a recursive list:

   (defun make-absolutely-sure-its-an-atom (obj)
     (not (consp obj)))

   (defun make-sure-its-an-atom (obj)
     (unless nil (make-absolutely-sure-its-an-atom obj)))

   (defun number-of-atoms (list)
     (let ((number 0))
       (dolist (x list number)
	 (if (atom x)
	   (if (make-sure-its-an-atom x) (incf number))))))

   (number-of-atoms '(1 2 3 ())) => 4
   (number-of-atoms '(1 2 3 (4))) => 3



   Sorry, absolutely couldn't resist! :-)

Or:

(count-if #'atom list)

Which gives:

(count-if #'atom '(1 2 3 ())) => 4
(count-if #'atom '(1 2 3 (4))) => 3

The issue of course is whether the list is recursive, and whether () ought
to be treated as nil or the empty list. If you want a simple solution to a
recursive list, it would look something like this (which is somewhat
inefficient, and can be presented with innumerable variations):

(defun count-atoms (list)
  (etypecase list
    (null 0)
    (atom 1)
    (cons
     (etypecase (car list)
       (null (count-atoms (cdr list)))
       (atom (1+ (count-atoms (cdr list))))
       (cons (+ (count-atoms (car list)) (count-atoms (cdr list))))))))

This also handles situations where the list ends in an atom other than
nil. So,

(count-atoms '(3 . 5)) => 2
(count-atoms '(3 () . 4)) => 2

Modifying this to interpret a nil in the middle of the list as an atom is
also quite straightforward, a simple variation:

(defun count-atoms (list)
  (etypecase list
    (null 0)
    (atom 1)
    (cons
     (etypecase (car list)
       (atom (1+ (count-atoms (cdr list))))
       (cons (+ (count-atoms (car list)) (count-atoms (cdr list))))))))

Now,

(count-atoms '(3 () . 4)) => 3

Note that the etypecase is an overkill. A simple typecase, with the last
statement's test turned into a t, would be sufficient, since everything in
lisp can be classified as either a cons or an atom.

Finally, the major inefficiency here is in the way recursion is
employed. A better way to do this would be to define a helper function for
count-atoms, that took an argument for the number of atoms that have
already been counted. This is left as an exercise.

Sunil
From: William Paul Vrotney
Subject: Re: lisp code
Date: 
Message-ID: <vrotneyEryn7F.C27@netcom.com>
In article <···············@clairmont.cc.gatech.edu> ·······@clairmont.cc.gatech.edu (Sunil Mishra) writes:
> 
> In article <·················@netcom.com> ·······@netcom.com (William Paul Vrotney) writes:
> 
>    In article <·················@quadrant.net> CH <······@quadrant.net> writes:
>    > 
>    > Hi, I am trying to write a lisp code which counts atoms within a list
>    > Can this be done only with lisp primitives? Thanks.
>    > --CH
>    > 

> 
>    Assuming the exercise is not to count atoms in a recursive list:
> 
>    (defun make-absolutely-sure-its-an-atom (obj)
>      (not (consp obj)))
> 
>    (defun make-sure-its-an-atom (obj)
>      (unless nil (make-absolutely-sure-its-an-atom obj)))
> 
>    (defun number-of-atoms (list)
>      (let ((number 0))
>        (dolist (x list number)
> 	 (if (atom x)
> 	   (if (make-sure-its-an-atom x) (incf number))))))
> 
>    (number-of-atoms '(1 2 3 ())) => 4
>    (number-of-atoms '(1 2 3 (4))) => 3
> 
> 
> 
>    Sorry, absolutely couldn't resist! :-)
> 
> Or:
> 
> (count-if #'atom list)
> 
> ...

This was meant as an elaborate joke.

There is a science to helping students cheat on their homework. :-)

-- 

William P. Vrotney - ·······@netcom.com