From: Scott
Subject: (LISP Newbie)
Date: 
Message-ID: <6MidneE8Xa3X37KjXTWcpA@comcast.com>
If I want to write the following in LISP using lists, car, cdr and
recursion, how would I do that?

A[6];     --- here this is a 6 element array, but in LISP, I just want a
list
for (i=0; i<4; i++)
  for (j=i+1; j<5; j++)
      some_function(A[i],A[j]);

This is a very small piece of a much larger homework project.  I know how to
do the project, (an AI game playing project), I'm just struggling to get off
the ground with the LISP language.

Are there any web sites that have C code and the LISP counterpart?  My book
is the George Luger, Artificial Intelligence text book, and he only has a
small bit of LISP.

Thanks for any help.
  -- Scott

From: Frank A. Adrian
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <ftKX9.287$6H1.79408@news.uswest.net>
Scott wrote:
> Are there any web sites that have C code and the LISP counterpart?  My
> book is the George Luger, Artificial Intelligence text book, and he only
> has a small bit of LISP.

Run, do not walk, to your local bookstore and pick up a copy of Paul 
Graham's "ANSI Common Lisp" or Peter Norvig's "Paradigms of AI 
Programming".  Either of them would be a worthwhile introduction to 
"thinking in Lisp".

If you are low on funds (being a student and all), I would recommend 
borrowing the same books from a library.  Otherwise, you could always go to 
the bookstore and attempt to steal them (not that I would advocate this 
solution).  If you want to get away with the Norvig book, I would recommend 
slightly bulkier clothing than if you were to attempt to purloin Graham's 
book (Norvig's is larger and harder to hide).  Tennis shoes to help run 
away quickly are also a good idea.

Good luck and welcome to the Lisp world.

faa
From: sv0f
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <none-2401031217260001@129.59.212.53>
In article <···················@news.uswest.net>, ·······@ancar.org wrote:

>Otherwise, you could always go to 
>the bookstore and attempt to steal them (not that I would advocate this 
>solution).  If you want to get away with the Norvig book, I would recommend 
>slightly bulkier clothing than if you were to attempt to purloin Graham's 
>book (Norvig's is larger and harder to hide).  Tennis shoes to help run 
>away quickly are also a good idea.
>
>Good luck and welcome to the Lisp world.

Welcome to the Lisp world.

The 1st rule of the Lisp world is: you do not talk about the Lisp world.

The 2nd rule of the Lisp world is: : YOU DO NO TALK ABOUT THE LISP WORLD!
From: Paul Wallich
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <pw-06EE1F.21094922012003@reader1.panix.com>
In article <······················@comcast.com>,
 "Scott" <·········@prodigy.xyz> wrote:

> If I want to write the following in LISP using lists, car, cdr and
> recursion, how would I do that?
> 
> A[6];     --- here this is a 6 element array, but in LISP, I just want a
> list
> for (i=0; i<4; i++)
>   for (j=i+1; j<5; j++)
>       some_function(A[i],A[j]);
> 
> This is a very small piece of a much larger homework project.  I know how to
> do the project, (an AI game playing project), I'm just struggling to get off
> the ground with the LISP language.

Using lists, car, cdr and recursion is kinda the wrong way for your 
professor to tell you to do this when there are perfectly good iterative 
primitives  (your choice of do, dotimes, or LOOP, all defined in the 
Common Lisp Hyper Spec, q.v.). But if you must, the obvious thing is to 
set up a  recursive function (steal someone's code for factorial) that 
walks down your list, at each step calling another recursive function 
that walks down the same damn list. Offsets, termination conditions, and 
passing the result of some-function back out should be pretty 
straightforward once you've understood how evaluation in Lisp proceeds.

good luck

paul
J
From: Frederick Crabbe
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <wzcu1fzftq9.fsf@crab.cs.usna.edu>
Paul Wallich <··@panix.com> writes:
> 
> Using lists, car, cdr and recursion is kinda the wrong way for your 
> professor to tell you to do this when there are perfectly good iterative 
> primitives  (your choice of do, dotimes, or LOOP, all defined in the 
> Common Lisp Hyper Spec, q.v.). 

You know, this is a pretty common sentiment expressed in this group,
and for the most part, I agree, BUT as a professor who teaches AI, I'd
like to point out that we often have ulterior motives.  In particular,
in many curricula, AI is the only place an undergraduate will be
exposed to functional programming(*).  In the past, I have explicitly
forbidden any iteration until at least 6 weeks into the semester.

In our newest version of the curriculum, I've arranged for the
Algorithms course to be taught in Scheme.  I hope that when the
current Sophomores get to AI, I can hand them Graham and say, "its
like Scheme, but better.  Read.  And now on to AI..." But we'll see.

cheers,
ric

(*)Yes, the Programming Languages course often talks about functional
programming, but unless they're using SICP, then it's usually limited
to writing "reverse" in ML.
From: Duane Rettig
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <4y95bv6aj.fsf@beta.franz.com>
Frederick Crabbe <······@usna.edu> writes:

> Paul Wallich <··@panix.com> writes:
> > 
> > Using lists, car, cdr and recursion is kinda the wrong way for your 
> > professor to tell you to do this when there are perfectly good iterative 
> > primitives  (your choice of do, dotimes, or LOOP, all defined in the 
> > Common Lisp Hyper Spec, q.v.). 
> 
> You know, this is a pretty common sentiment expressed in this group,
> and for the most part, I agree, BUT as a professor who teaches AI, I'd
> like to point out that we often have ulterior motives.  In particular,
> in many curricula, AI is the only place an undergraduate will be
> exposed to functional programming(*).  In the past, I have explicitly
> forbidden any iteration until at least 6 weeks into the semester.

The trouble is that if a student has learned a "complete" traditional
programming language like C, and then courses are added to teach
additional concepts like FP, symbolic processing, interactivity,
closures, etc, then the student gets the idea that the language they're
now being taught doesn't _have_ traditional constructs, which at least
for CL isn't true.  Since it is then almost certainly Scheme they are
learning (which is to say "this is my obligatory LISP course"),
then they get a warped view of what the Lisp gestalt really is.

> In our newest version of the curriculum, I've arranged for the
> Algorithms course to be taught in Scheme.  I hope that when the
> current Sophomores get to AI, I can hand them Graham and say, "its
> like Scheme, but better.  Read.  And now on to AI..." But we'll see.

That's fine, but wouldn't it be easier to start with CL and then
when you get to Graham say "it's just like CL..."?  And then when
you go on to AI, the stuents can actually start learning AI instead
of half-learning a plethora of new languages and assimilating
neither the languages nor the AI...

> (*)Yes, the Programming Languages course often talks about functional
> programming, but unless they're using SICP, then it's usually limited
> to writing "reverse" in ML.

Learning the tools by reimplementing the tools is one style of
teaching, but it's rudimentary, and once a student gets to upper
levels, he should already know the tools he needs thoroughly.

When you take a course in carpentry, you may disassemble a router
partially to understand the workings and the maintenance, but you
would never waste time in a class designed to teach you how to _use_
a router by _building_ a router.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: Paul Wallich
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <pw-62E3C4.18081023012003@reader1.panix.com>
In article <·············@beta.franz.com>,
 Duane Rettig <·····@franz.com> wrote:

> Frederick Crabbe <······@usna.edu> writes:
> 
> > Paul Wallich <··@panix.com> writes:
> > > 
> > > Using lists, car, cdr and recursion is kinda the wrong way for your 
> > > professor to tell you to do this when there are perfectly good iterative 
> > > primitives  (your choice of do, dotimes, or LOOP, all defined in the 
> > > Common Lisp Hyper Spec, q.v.). 
> > 
> > You know, this is a pretty common sentiment expressed in this group,
> > and for the most part, I agree, BUT as a professor who teaches AI, I'd
> > like to point out that we often have ulterior motives.  In particular,
> > in many curricula, AI is the only place an undergraduate will be
> > exposed to functional programming(*).  In the past, I have explicitly
> > forbidden any iteration until at least 6 weeks into the semester.
> 
> The trouble is that if a student has learned a "complete" traditional
> programming language like C, and then courses are added to teach
> additional concepts like FP, symbolic processing, interactivity,
> closures, etc, then the student gets the idea that the language they're
> now being taught doesn't _have_ traditional constructs, which at least
> for CL isn't true.  Since it is then almost certainly Scheme they are
> learning (which is to say "this is my obligatory LISP course"),
> then they get a warped view of what the Lisp gestalt really is.

Furthermore, if you're going to insist that students use only the 
functional aspects of Lisp, then I would hope you give them exercises 
that demonstrate why those functional bits are useful, rather than 
telling them (as the original poster's prof apparently did) to use 
recursion on a task tailor-made for iteration. (Heck, if one were going 
to insist on functional programming, one of the mapping functions would 
have been at least slightly less ugly.) 

paul      with some handplanes and chisels to tune
From: Frederick Crabbe
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <wzclm1aezxt.fsf@crab.cs.usna.edu>
Paul Wallich <··@panix.com> writes:
>
> Furthermore, if you're going to insist that students use only the 
> functional aspects of Lisp, then I would hope you give them exercises 
> that demonstrate why those functional bits are useful, rather than 
> telling them (as the original poster's prof apparently did) to use 
> recursion on a task tailor-made for iteration. (Heck, if one were going 
> to insist on functional programming, one of the mapping functions would 
> have been at least slightly less ugly.) 
> 

OK, I agree that what the original poster's prof apparently did is not
the correct thing to do.  But it isn't so easy to come up with new AI
based projects every semester, and ensure they always fit well into
FP.  I try to make the first few weeks assignments all nice tree
search, but I know that in the past I've screwed that one up.  

cheers,
ric
From: Frederick Crabbe
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <wzcptqmf0d8.fsf@crab.cs.usna.edu>
Duane Rettig <·····@franz.com> writes:

> Frederick Crabbe <······@usna.edu> writes:
> 
> > Paul Wallich <··@panix.com> writes:
> > > 
> > > Using lists, car, cdr and recursion is kinda the wrong way for your 
> > > professor to tell you to do this when there are perfectly good iterative 
> > > primitives  (your choice of do, dotimes, or LOOP, all defined in the 
> > > Common Lisp Hyper Spec, q.v.). 
> > 
> > You know, this is a pretty common sentiment expressed in this group,
> > and for the most part, I agree, BUT as a professor who teaches AI, I'd
> > like to point out that we often have ulterior motives.  In particular,
> > in many curricula, AI is the only place an undergraduate will be
> > exposed to functional programming(*).  In the past, I have explicitly
> > forbidden any iteration until at least 6 weeks into the semester.
> 
> The trouble is that if a student has learned a "complete" traditional
> programming language like C, and then courses are added to teach
> additional concepts like FP, symbolic processing, interactivity,
> closures, etc, then the student gets the idea that the language they're
> now being taught doesn't _have_ traditional constructs, which at least
> for CL isn't true.  Since it is then almost certainly Scheme they are
> learning (which is to say "this is my obligatory LISP course"),
> then they get a warped view of what the Lisp gestalt really is.

Oh yes, I agree completely.  The trouble is that we have too many
things we want to teach, and too little time to do it in.  "Teaching
Lisp Properly" often ends up somewhere after "AI" and "Exposure to FP"
in the priority queue.  This bothered me enough to push for the
curriculum change.

> 
> > In our newest version of the curriculum, I've arranged for the
> > Algorithms course to be taught in Scheme.  I hope that when the
> > current Sophomores get to AI, I can hand them Graham and say, "its
> > like Scheme, but better.  Read.  And now on to AI..." But we'll see.
> 
> That's fine, but wouldn't it be easier to start with CL and then
> when you get to Graham say "it's just like CL..."?  And then when
> you go on to AI, the stuents can actually start learning AI instead
> of half-learning a plethora of new languages and assimilating
> neither the languages nor the AI...

It sure would be easier.  Unfortunately, there are certain political
realities that prevent "Lisp as an intro language".  I managed to get
that idea on the agenda for the curriculum change, but I was shot
down.  One issue many don't think about is that CS departments justify
staffing levels based on the service provided to other departments by
teaching intro programming.  If we switched to Lisp, EE and MechE
would teach their own courses in C/C++, and we would lose the leverage
to hire faculty and purchase lab equipment.  I think that is the same
everywhere. 

> 
> > (*)Yes, the Programming Languages course often talks about functional
> > programming, but unless they're using SICP, then it's usually limited
> > to writing "reverse" in ML.
> 
> Learning the tools by reimplementing the tools is one style of
> teaching, but it's rudimentary, and once a student gets to upper
> levels, he should already know the tools he needs thoroughly.
> 
> When you take a course in carpentry, you may disassemble a router
> partially to understand the workings and the maintenance, but you
> would never waste time in a class designed to teach you how to _use_
> a router by _building_ a router.

Again, I agree.  But if I'm teaching woodworking and I have two
lectures devoted to tools, I might likely skip routers and table saws
and joiners and laser guided miter boxes etc, to stick to a chisel and
a hand saw, or for that matter, a swiss army knife (I assume we're
referring to GLS's introduction to "Scheme and the Art of
Programming"?)

In the end, there are too many demands and too few resources to do
things the way we all know is the "right" way.

cheers,
ric
From: Larry Clapp
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <14nn0b.3v5.ln@127.0.0.1>
In article <······················@comcast.com>, Scott wrote:
<snip>
> Are there any web sites that have C code and the LISP
> counterpart?  My book is the George Luger, Artificial
> Intelligence text book, and he only has a small bit of LISP.

This (http://theclapp.blog-city.com) may help.  Or it may not.
:)  I compare Java and Lisp (though perhaps not very well :).
Start here: http://theclapp.blog-city.com/readblog.cfm?BID=9347.
I only started a few weeks ago, but it might help a little.

Even better, though, consider reading some of the online
tutorials.  Try http://www.lisp.org/table/learn.htm for some
links.

-- Larry
From: Scott
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <dvqcndYdesWg-7KjXTWcoA@comcast.com>
> Even better, though, consider reading some of the online
> tutorials.  Try http://www.lisp.org/table/learn.htm for some
> links.
>
> -- Larry
>

Thanks for the tutorial link.  I've already found some good examples in
there.
  -- Scott
From: Barry Margolin
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <l%HX9.68$rQ6.1228@paloalto-snr1.gtei.net>
In article <······················@comcast.com>,
Scott <·········@prodigy.xyz> wrote:
>If I want to write the following in LISP using lists, car, cdr and
>recursion, how would I do that?
>
>A[6];     --- here this is a 6 element array, but in LISP, I just want a
>list
>for (i=0; i<4; i++)
>  for (j=i+1; j<5; j++)
>      some_function(A[i],A[j]);
>
>This is a very small piece of a much larger homework project.  I know how to
>do the project, (an AI game playing project), I'm just struggling to get off
>the ground with the LISP language.
>
>Are there any web sites that have C code and the LISP counterpart?  My book
>is the George Luger, Artificial Intelligence text book, and he only has a
>small bit of LISP.

The Lisp counterpart to the above code probably wouldn't use lists or
recursion.  It could be written that way, but it wouldn't be the most
natural way to do it.  In fact, the most obvious way to do it would be
basically the same as the C version:

(defvar a (make-array 6))
(dotimes (i 5)
  (do ((j (1+ i) (1+ j)))
      ((>= j 5))
    (some-function (aref a i) (aref a j))))

However, doing it with lists and recursion could look like this:

(defun process-list-1 (list)
  (unless (endp (cdr list))
    (process-list-2 (car list) (cdr list))
    (process-list-1 (cdr list))))

(defun process-list-2 (item list)
  (unless (endp list)
    (some-function item (car list))
    (process-list-2 item (cdr list))))
        

-- 
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: Scott
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <df-dndveTM-2-LKjXTWc3Q@comcast.com>
>
> However, doing it with lists and recursion could look like this:
>
> (defun process-list-1 (list)
>   (unless (endp (cdr list))
>     (process-list-2 (car list) (cdr list))
>     (process-list-1 (cdr list))))
>
> (defun process-list-2 (item list)
>   (unless (endp list)
>     (some-function item (car list))
>     (process-list-2 item (cdr list))))
>

Thanks, that's an obvious soloution when I see it.  I kept trying to come up
with a way using a single function.
  -- Scott
From: Daniel Barlow
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <87y95c8acj.fsf@noetbook.telent.net>
Barry Margolin <······@genuity.net> writes:

> The Lisp counterpart to the above code probably wouldn't use lists or
> recursion.  It could be written that way, but it wouldn't be the most
> natural way to do it.  In fact, the most obvious way to do it would be
> basically the same as the C version:
>
> (defvar a (make-array 6))
> (dotimes (i 5)
>   (do ((j (1+ i) (1+ j)))
>       ((>= j 5))
>     (some-function (aref a i) (aref a j))))

They look like lists to me.  Some of them are nested lists, even, so
there's likely some kind of recursion involved in reading them.  I say
this meets the requirement :-)


-dan

-- 

   http://www.cliki.net/ - Link farm for free CL-on-Unix resources 
From: Kenny Tilton
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <3E2F8075.5020407@nyc.rr.com>
Scott wrote:
> If I want to write the following in LISP using lists, car, cdr and
> recursion, how would I do that?
> 
> A[6];     --- here this is a 6 element array, but in LISP, I just want a
> list
> for (i=0; i<4; i++)
>   for (j=i+1; j<5; j++)
>       some_function(A[i],A[j]);
> 
> This is a very small piece of a much larger homework project.  I know how to
> do the project, (an AI game playing project), I'm just struggling to get off
> the ground with the LISP language.

I hear you saying that you understand programming well enough to create 
a software analogue for AI in a game scenario, but not well enough to 
translate elementary looping between languages. Both of which have 
similar looping built-ins. Oooooh-kay.

> 
> Are there any web sites that have C code and the LISP counterpart?  My book
> is the George Luger, Artificial Intelligence text book, and he only has a
> small bit of LISP.

And you can post an intelligent question in comp.lang.lisp but you do 
not know about Google? Rrrrriiiigggghhhhhttt....

> 
> Thanks for any help.

Quit your day job, that's funny stuff.

:)

-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Cells let us walk, talk, think, make love and realize
  the bath water is cold." -- Lorraine Lee Cudmore
From: Matt Curtin
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <86y95cru3l.fsf@rowlf.interhack.net>
"Scott" <·········@prodigy.xyz> writes:

> Are there any web sites that have C code and the LISP counterpart?
> My book is the George Luger, Artificial Intelligence text book, and
> he only has a small bit of LISP.

I would recommend getting a good book on Lisp to help get a sense for
what is idiomatic (or "natural").  There just aren't a lot of direct
comparisons between C and Lisp that are very helpful.

The book I would recommend (and that I use for my Lisp course at Ohio
State) is Paul Graham's /ANSI Common Lisp/.  (The book is a very easy
read, and is brief but wonderfully full of goodies to help you start
programming in Lisp.)  Paul also has some good stuff about Lisp up on
his site at http://www.paulgraham.com/.  The Association of Lisp Users
has some good stuff at http://www.lisp.org/.

The reason I recommend going this route instead of just answering your
question directly :-) is that different languages have different
world-views (they're optimized for different criteria, and are
influenced by different philosophies of design).  So you can directly
translate one to the other, but it will feel foreign to someone more
familiar with the target language, and will tend not to use the
strengths of the target language.  If all of that has happened, you
might as well have just used the source language for the whole thing
in the first place. :-)

Having written that, here is a fairly direct translation, which is
quite ugly to Lisp, for reasons described below.

> for (i=0; i<4; i++)
>   for (j=i+1; j<5; j++)
>       some_function(A[i],A[j]);

(do ((i 0 (1+ i)))
    ((not (< i 4)) nil)
  (do ((j (1+ i) (1+ j)))
      ((not (< j 5)) nil)
    (some-function (nth i A) (nth j A))))

Problems here are that the outermost DO form is really overkill for
the job we're accomplishing; DOTIMES would work just fine.  Using the
NOT forms to construct the exit clauses is weird, but I did it because
that's what it means in C.  Finally, it's not usually idiomatic to
have some external bit of data that a function will work on.
Normally, we'd pass something info a function, and that function would
returned new data, rather than setting some variable (like A) in a
larger scope.

-- 
Matt Curtin, CISSP, IAM, INTP.  Keywords: Lisp, Unix, Internet, INFOSEC.
Founder, Interhack Corporation +1 614 545 HACK http://web.interhack.com/
Author of /Developing Trust: Online Privacy and Security/ (Apress, 2001)
From: Bernhard Pfahringer
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <f2c94ab2.0301231743.7be53915@posting.google.com>
Matt Curtin <········@interhack.net> wrote in message news:<··············@rowlf.interhack.net>...

>
>> for (i=0; i<4; i++)
>>   for (j=i+1; j<5; j++)
>>       some_function(A[i],A[j]);
>
>(do ((i 0 (1+ i)))
>    ((not (< i 4)) nil)
>  (do ((j (1+ i) (1+ j)))
>      ((not (< j 5)) nil)
>    (some-function (nth i A) (nth j A))))
>

Never use nth in this fashion: it will turn what should be a perfectly
linear algorithm into a quadratic one. A loop-based version for lists:

(loop for (x . tail) on list do
   (loop for y in tail do
      (some-function x y)))

cheers, Bernhard
From: Thomas A. Russ
Subject: Re: (LISP Newbie)
Date: 
Message-ID: <ymiel727z1l.fsf@sevak.isi.edu>
"Scott" <·········@prodigy.xyz> writes:

> 
> If I want to write the following in LISP using lists, car, cdr and
> recursion, how would I do that?
> 
> A[6];     --- here this is a 6 element array, but in LISP, I just want a
> list

Actually, if you really want to have random access to the elements, then
you want a 6 element array in Lisp as well.  Just because Lisp makes it
easy (codewise) to access an arbitrary element of a list, doesn't mean
it is efficient (runtime-wise).

> for (i=0; i<4; i++)
>   for (j=i+1; j<5; j++)
>       some_function(A[i],A[j]);

(defvar *A* (make-array 6))

(loop for i from 0 below 4
   do (loop for j from (1+ i) below 5
         do (some-function (aref *a* i) (aref *a* j))))

Or if you insist on doing this with a list:

(loop for (first-arg . rest) on *A*
      as i from 0 below 4
      do (loop for second-arg in rest
               do (some-function first-arg second-arg)))


> Are there any web sites that have C code and the LISP counterpart?  My book
> is the George Luger, Artificial Intelligence text book, and he only has a
> small bit of LISP.

This is a very poor way to learn Lisp.  That is because you will never
learn the Lisp programming idioms by thinking in C and translating.  You
will just end up trying to write C in Lisp and become frustrated.  To
effectively program in Lisp, you need to learn the Lisp way of thinking.



-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu