From: ········@cbbrowne.com
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <dnLD7.27076$6h5.1494533@news20.bellglobal.com>
On Wed, 31 Oct 2001 13:02:01 +0900, the world broke into rejoicing as
[identity elided to protect the likely-guilty...]
> hello, nice to meet you. i am a student
> 
> nowdays, i am studying about lisp (I USEING XLISP or common lisp)
> 
> i have a lisp problem, but i can't solve it
> 
> so, i belive , i wanner belive, you can help me...
> 
> please, please, i wanner receive your reply.
> 
> thank you for your reading.
>  
> I wanner define a function, "nth-item"
> e.g.   (nth-item 3 '(a b c d e f))      returns  c
>        (nth-item 2 '(a b c d e f))      returns  b
>        (nth-item 5 '(a b c d e f))      returns  e

One reasonable implementation might be coded as follows:

(defun nth-item (item list)
  (let ((foo nil))
    (loop 
      for bar in list
      for zoo 
      until (= zoo item)
      do (setf foo (make-symbol (string-downcase (symbol-name bar)))))
    foo))

(defun runtest ()
  (let ((full-list '(a b c d e f g)))
    (loop
	for i from 2 to 6
	do
	(format t "Item ~D from ~A is ~A~%" 
		i full-list (nth-item i full-list)))))

(runtest)

The results of a run:

Item 2 from (A B C D E F G) is b
Item 3 from (A B C D E F G) is c
Item 4 from (A B C D E F G) is d
Item 5 from (A B C D E F G) is e
Item 6 from (A B C D E F G) is f

I'm thinking that this doesn't _quite_ satisfy the expectations, in
that (A B C D E F G) are left in upper case.

Can somebody suggest a method involving READTABLE modifications that
would accomplish that?  I don't think I'm quite up to that.  I'd be
glad to pass it on the student in question; I'm sure the folks marking
the question will be more than pleased to see such creative answers
coming from their students :-).
--
(reverse (concatenate 'string ··········@" "enworbbc"))
http://www.cbbrowne.com/info/commonlisp.html
"Ah,  fall  - when  leaves  turn  to  burnished colors  upon  darkling
branches,  collars are  turned  up  against a  wind  which murmurs  of
winter, and homework assignments appear on Usenet.  <sigh>"
-- Bob Jarvis

From: Erik Naggum
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <3213492916086542@naggum.net>
* ········@cbbrowne.com
| Can somebody suggest a method involving READTABLE modifications that
| would accomplish that?

  Huh?  Bind *print-case* to :downcase, and the printer will print symbol
  names in all lower-case.  With all the clamoring some people engage in to
  point out how ugly Common Lisp is with upper-case symbol names, this
  simple printer control variable setting at least takes care of the
  problem of seeing the supposedly ugly upper-case letters in replies.

///
-- 
  Norway is now run by a priest from the fundamentalist Christian People's
  Party, the fifth largest party representing one eighth of the electorate.
-- 
  Carrying a Swiss Army pocket knife in Oslo, Norway, is a criminal offense.
From: Coby Beck
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <6VLD7.533638$8c3.89761003@typhoon.tampabay.rr.com>
<········@cbbrowne.com> wrote in message
····························@news20.bellglobal.com...
> On Wed, 31 Oct 2001 13:02:01 +0900, the world broke into rejoicing as
> [identity elided to protect the likely-guilty...]
> > hello, nice to meet you. i am a student
> > I wanner define a function, "nth-item"
> > e.g.   (nth-item 3 '(a b c d e f))      returns  c
> >        (nth-item 2 '(a b c d e f))      returns  b
> >        (nth-item 5 '(a b c d e f))      returns  e
>
> One reasonable implementation might be coded as follows:
>
> (defun nth-item (item list)
>   (let ((foo nil))
>     (loop
>       for bar in list
>       for zoo
>       until (= zoo item)
>       do (setf foo (make-symbol (string-downcase (symbol-name bar)))))
>     foo))
>

do you really want something that *only* works for lists of symbols?  and only
lowercased symbols?  How about:

(defun nth-item (index list)
      (loop for elt in list
               for i upfrom 1
               until (= i index)
               finally (return elt)))

to borrow from your initial looping design.

I imagine
(defun nth-item (index list) (nth index list))
is not allowed  : )

BTW the proposed solution does not do zero based indexing as does nth, you
could of course just make i upfrom 0 in the loop....

Coby
--
(remove #\space "coby . beck @ opentechgroup . com")
From: ········@acm.org
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <G5MD7.27208$6h5.1526727@news20.bellglobal.com>
"Coby Beck" <·····@mercury.bc.ca> writes:
> <········@cbbrowne.com> wrote in message
> ····························@news20.bellglobal.com...
> > On Wed, 31 Oct 2001 13:02:01 +0900, the world broke into rejoicing as
> > [identity elided to protect the likely-guilty...]
> > > hello, nice to meet you. i am a student
> > > I wanner define a function, "nth-item"
> > > e.g.   (nth-item 3 '(a b c d e f))      returns  c
> > >        (nth-item 2 '(a b c d e f))      returns  b
> > >        (nth-item 5 '(a b c d e f))      returns  e
> >
> > One reasonable implementation might be coded as follows:
> >
> > (defun nth-item (item list)
> >   (let ((foo nil))
> >     (loop
> >       for bar in list
> >       for zoo
> >       until (= zoo item)
> >       do (setf foo (make-symbol (string-downcase (symbol-name bar)))))
> >     foo))
> >

> do you really want something that *only* works for lists of symbols?
> and only lowercased symbols?  How about:

> (defun nth-item (index list)
>       (loop for elt in list
>                for i upfrom 1
>                until (= i index)
>                finally (return elt)))
> 
> to borrow from your initial looping design.

The only problem is that if it returns a symbol, it doesn't return a
lower cased symbol, as the student described as the expected return
value :-).

> I imagine
> (defun nth-item (index list) (nth index list))
> is not allowed  : )

> BTW the proposed solution does not do zero based indexing as does
> nth, you could of course just make i upfrom 0 in the loop....

The question definitely didn't address based on zero...

Another reasonable answer _could_ be:

(defun nth-item (index list)
   (nth (1+ index) list))

   or perhaps:

(defmethod nth-item ((number index) (list list))
   (nth (1+ index) list))

I'm looking forward to seeing a SERIES-based proposal :-).
-- 
(concatenate 'string "chris" ·@cbbrowne.com")
http://www.cbbrowne.com/info/linux.html
"The cost of living has just gone up another dollar a quart."  
-- W.C. Fields
From: Coby Beck
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <YxSD7.545775$8c3.90107246@typhoon.tampabay.rr.com>
<········@acm.org> wrote in message
····························@news20.bellglobal.com...
> "Coby Beck" <·····@mercury.bc.ca> writes:
> > <········@cbbrowne.com> wrote in message
> > ····························@news20.bellglobal.com...
> > > On Wed, 31 Oct 2001 13:02:01 +0900, the world broke into rejoicing as
> > > [identity elided to protect the likely-guilty...]
> > > > hello, nice to meet you. i am a student
> > > > I wanner define a function, "nth-item"
> > > > e.g.   (nth-item 3 '(a b c d e f))      returns  c
> > > >        (nth-item 2 '(a b c d e f))      returns  b
> > > >        (nth-item 5 '(a b c d e f))      returns  e
> > >

> > do you really want something that *only* works for lists of symbols?
> > and only lowercased symbols?  How about:
>
> > (defun nth-item (index list)
> >       (loop for elt in list
> >                for i upfrom 1
> >                until (= i index)
> >                finally (return elt)))
> >
> > to borrow from your initial looping design.
>
> The only problem is that if it returns a symbol, it doesn't return a
> lower cased symbol, as the student described as the expected return
> value :-).

This has to do with the printed representation of the return value, not the
return value itself.  Having gone through similar exercises in similar
beginner's lisp courses, I can virtually guarantee printcase is not relevant to
the assignment.

Coby
--
(remove #\space "coby . beck @ opentechgroup . com")
From: ········@acm.org
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <wRTD7.27573$6h5.1684966@news20.bellglobal.com>
"Coby Beck" <·····@mercury.bc.ca> writes:
> <········@acm.org> wrote in message
> ····························@news20.bellglobal.com...
> > "Coby Beck" <·····@mercury.bc.ca> writes:
> > > <········@cbbrowne.com> wrote in message
> > > ····························@news20.bellglobal.com...
> > > > On Wed, 31 Oct 2001 13:02:01 +0900, the world broke into rejoicing as
> > > > [identity elided to protect the likely-guilty...]
> > > > > hello, nice to meet you. i am a student
> > > > > I wanner define a function, "nth-item"
> > > > > e.g.   (nth-item 3 '(a b c d e f))      returns  c
> > > > >        (nth-item 2 '(a b c d e f))      returns  b
> > > > >        (nth-item 5 '(a b c d e f))      returns  e
> > > >
> 
> > > do you really want something that *only* works for lists of symbols?
> > > and only lowercased symbols?  How about:
> >
> > > (defun nth-item (index list)
> > >       (loop for elt in list
> > >                for i upfrom 1
> > >                until (= i index)
> > >                finally (return elt)))
> > >
> > > to borrow from your initial looping design.
> >
> > The only problem is that if it returns a symbol, it doesn't return a
> > lower cased symbol, as the student described as the expected return
> > value :-).

> This has to do with the printed representation of the return value,
> not the return value itself.  Having gone through similar exercises
> in similar beginner's lisp courses, I can virtually guarantee
> printcase is not relevant to the assignment.

I quite entirely realize that; the point was to take into
consideration as many spurious specification elements as possible...
-- 
(reverse (concatenate 'string ··········@" "enworbbc"))
http://www.cbbrowne.com/info/finances.html
Rules of the  Evil Overlord #109. "I will see to  it that plucky young
lads/lasses in  strange clothes  and with the  accent of  an outlander
shall REGULARLY climb  some monument in the main  square of my capital
and  denounce me,  claim to  know the  secret of  my power,  rally the
masses to rebellion, etc. That way, the citizens will be jaded in case
the real thing ever comes along." <http://www.eviloverlord.com/>
From: Lieven Marchand
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <m3r8rjk64q.fsf@localhost.localdomain>
········@acm.org writes:

> Another reasonable answer _could_ be:
> 
> (defun nth-item (index list)
>    (nth (1+ index) list))
> 
>    or perhaps:
> 
> (defmethod nth-item ((number index) (list list))
>    (nth (1+ index) list))
>

That's no fun.

(defmethod nth-item ((index integer) (list cons))
  (nth-item (1- index) (rest list)))

(defmethod nth-item ((index (eql 1)) (list cons))
  (first list))

Recursion is very important in LISP.
 
> I'm looking forward to seeing a SERIES-based proposal :-).

It's not a very series like question but here goes:

(defun nth-item (index list)
  (collect-last (until (map-fn 'integer #'(lambda (x) (> x index)) 
                          (scan-range :from 1))
                       (scan list))))

-- 
Lieven Marchand <···@wyrd.be>
She says, "Honey, you're a Bastard of great proportion."
He says, "Darling, I plead guilty to that sin."
Cowboy Junkies -- A few simple words
From: Raymond Toy
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <4n7ktbo7l3.fsf@rtp.ericsson.se>
>>>>> "Lieven" == Lieven Marchand <···@wyrd.be> writes:

    Lieven> ········@acm.org writes:
    >> I'm looking forward to seeing a SERIES-based proposal :-).

    Lieven> It's not a very series like question but here goes:

    Lieven> (defun nth-item (index list)
    Lieven>   (collect-last (until (map-fn 'integer #'(lambda (x) (> x index)) 
    Lieven>                           (scan-range :from 1))
    Lieven>                        (scan list))))

I think this is easier to read and understand:

(defun nth-item (index list)
  (collect-first (subseries (scan list) index)))

But there are other options like

(defun nth-item (index list)
  (collect-last (choose (scan-range :upto index) (scan list))))

etc.

Ray
From: Paul Foley
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <m2668vl65o.fsf@mycroft.actrix.gen.nz>
On 31 Oct 2001 15:52:40 -0500, Raymond Toy wrote:

>>>>>> "Lieven" == Lieven Marchand <···@wyrd.be> writes:
Lieven> ········@acm.org writes:
>>> I'm looking forward to seeing a SERIES-based proposal :-).

Lieven> It's not a very series like question but here goes:

Lieven> (defun nth-item (index list)
Lieven>   (collect-last (until (map-fn 'integer #'(lambda (x) (> x index)) 
Lieven>                           (scan-range :from 1))
Lieven>                        (scan list))))

> I think this is easier to read and understand:

> (defun nth-item (index list)
>   (collect-first (subseries (scan list) index)))

> But there are other options like

> (defun nth-item (index list)
>   (collect-last (choose (scan-range :upto index) (scan list))))

 (collect-nth (1- item) (scan list))

-- 
The power of accurate observation is commonly called cynicism by those
who have not got it.
                                                    -- George Bernard Shaw
(setq reply-to
  (concatenate 'string "Paul Foley " "<mycroft" '(··@) "actrix.gen.nz>"))
From: ········@acm.org
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <6a_D7.29512$az4.2429419@news20.bellglobal.com>
Lieven Marchand <···@wyrd.be> writes:
> ········@acm.org writes:
> 
> > Another reasonable answer _could_ be:
> > 
> > (defun nth-item (index list)
> >    (nth (1+ index) list))
> > 
> >    or perhaps:
> > 
> > (defmethod nth-item ((number index) (list list))
> >    (nth (1+ index) list))
> >
> 
> That's no fun.
> 
> (defmethod nth-item ((index integer) (list cons))
>   (nth-item (1- index) (rest list)))
> 
> (defmethod nth-item ((index (eql 1)) (list cons))
>   (first list))
> 
> Recursion is very important in LISP.

Thank you; that's fabulous; a much Better Answer than I had thought
of, and has the merit of conforming to the usual expectation that
solutions to problem must involve recursions, 'cause that's all Lisp
is _truly_ about.  (Flames can head to NIL, specifically the NIL in
package JOKING...)

> > I'm looking forward to seeing a SERIES-based proposal :-).
> 
> It's not a very series like question but here goes:
> 
> (defun nth-item (index list)
>   (collect-last (until (map-fn 'integer #'(lambda (x) (> x index)) 
>                           (scan-range :from 1))
>                        (scan list))))

Every question should be a SERIES-like question :-).
-- 
(concatenate 'string "cbbrowne" ·@cbbrowne.com")
http://www.ntlug.org/~cbbrowne/linux.html
Whatever is contradictory or paradoxical is called the back of God.
His face, where all exists in perfect harmony, cannot be seen by man.
From: Gabe Garza
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <4rogfhtt.fsf@kynopolis.org>
········@cbbrowne.com writes:

> On Wed, 31 Oct 2001 13:02:01 +0900, the world broke into rejoicing as
> [identity elided to protect the likely-guilty...]
> > hello, nice to meet you. i am a student

> > I wanner define a function, "nth-item"
> > e.g.   (nth-item 3 '(a b c d e f))      returns  c
> >        (nth-item 2 '(a b c d e f))      returns  b
> >        (nth-item 5 '(a b c d e f))      returns  e
> 
> One reasonable implementation might be coded as follows:
> 
> (defun nth-item (item list)
>   (let ((foo nil))
>     (loop 
>       for bar in list
>       for zoo 
>       until (= zoo item)
>       do (setf foo (make-symbol (string-downcase (symbol-name bar)))))
>     foo))

This is completely innapropriate.  A student taking a course in Lisp
is not expected to use loop, or any other form of iteration.  On the
other hand, higher-order functions and anonymous functions
automatically make a program more Lisp-like and in sufficient
quantities must earn bonus points.

To that end, my solution uses two calls to MAPCAR, one call to REDUCE,
and 4 LAMBDA's.  Closure creation is O(n), where n is the length of
the list.  It gracefully handles out-of-bounds by returning NIL.

(defun nth-item (index list)
  (reduce
   (lambda (a b) (or a b))
   (mapcar
    (lambda (thunk)
      (funcall thunk index))
    (let ((pos 1))
      (mapcar
       (lambda (item)
	 (let ((j pos))
	   (prog1
	       (lambda (i)
		 (when (= i j)
		   item))
	     (incf pos)))) list))))) 
   
Gabe Garza
From: Paul Foley
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <m2elnkkv5f.fsf@mycroft.actrix.gen.nz>
On Wed, 31 Oct 2001 04:47:05 GMT, cbbrowne  wrote:

> On Wed, 31 Oct 2001 13:02:01 +0900, the world broke into rejoicing as
> [identity elided to protect the likely-guilty...]
>> 
>> I wanner define a function, "nth-item"
>> e.g.   (nth-item 3 '(a b c d e f))      returns  c
>> (nth-item 2 '(a b c d e f))      returns  b
>> (nth-item 5 '(a b c d e f))      returns  e

> One reasonable implementation might be coded as follows:

> (defun nth-item (item list)
>   (let ((foo nil))
>     (loop
>       for bar in list
>       for zoo
>       until (= zoo item)
>       do (setf foo (make-symbol (string-downcase (symbol-name bar)))))
>     foo))

Or just (nth (1- item) list), but if you're going to do homework for
people, at least give the solution as something like

  (defun nth-item (item list)
    (funcall ((lambda (lambda) ((lambda (funcall) (funcall lambda (lambda
    (cdr - lambda) (funcall (funcall funcall funcall) cdr - lambda))))
    (lambda (funcall) (funcall lambda (lambda (= cond car) (funcall
    (funcall funcall funcall) = cond car)))))) (lambda (funcall) (lambda
    (cond lambda car) (cond ((= cond car) (car lambda)) (nil lambda (car
    nil) t funcall) (t (funcall funcall (- cond car) (cdr lambda) car))
    (nil (- (car funcall)) t lambda))))) item list 1))

[that way, if they manage to convince the teacher they wrote it, they
deserve credit :-)]

> I'm thinking that this doesn't _quite_ satisfy the expectations, in
> that (A B C D E F G) are left in upper case.

As they should be.  If you want them to print in lower case, set the
printer control variables appropriately.

> Can somebody suggest a method involving READTABLE modifications that
> would accomplish that?  I don't think I'm quite up to that.  I'd be

Yuck.  No.  (setq *print-case* :downcase)

-- 
The power of accurate observation is commonly called cynicism by those
who have not got it.
                                                    -- George Bernard Shaw
(setq reply-to
  (concatenate 'string "Paul Foley " "<mycroft" '(··@) "actrix.gen.nz>"))
From: David Hanley
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <281e82b2.0111011619.6437aeb8@posting.google.com>
Oh, come on.  Your suggestions are hardly beleivable LISP because they
do not invoke EVAL.

(defun n-th( index set ) 
  (setf set (cons 'list set))
  (setf index (- index 1))
  (dotimes (i index)
    (setf set (cons 'cdr (list set))))
  (setf set (cons 'car (list set)))
  (eval set))

dave
From: Daniel Barlow
Subject: Re: lisp question ~ ^*^
Date: 
Message-ID: <87elnhs0zr.fsf@noetbook.telent.net>
···········@yahoo.com (David Hanley) writes:

> Oh, come on.  Your suggestions are hardly beleivable LISP because they
> do not invoke EVAL.
> 
> (defun n-th( index set ) 
>   (setf set (cons 'list set))
>   (setf index (- index 1))
>   (dotimes (i index)
>     (setf set (cons 'cdr (list set))))
>   (setf set (cons 'car (list set)))
>   (eval set))

Shouldn't this all be in UPPERCASE?


-dan

-- 

  http://ww.telent.net/cliki/ - Link farm for free CL-on-Unix resources