Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

lisp question ~ ^*^

14 views
Skip to first unread message

cbbr...@cbbrowne.com

unread,
Oct 30, 2001, 11:47:05 PM10/30/01
to
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 "gro.gultn@" "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

Erik Naggum

unread,
Oct 30, 2001, 11:55:17 PM10/30/01
to
* cbbr...@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.

Coby Beck

unread,
Oct 31, 2001, 12:23:14 AM10/31/01
to

<cbbr...@cbbrowne.com> wrote in message
news:dnLD7.27076$6h5.1...@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")


cbbr...@acm.org

unread,
Oct 31, 2001, 12:36:38 AM10/31/01
to

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

Gabe Garza

unread,
Oct 31, 2001, 1:22:09 AM10/31/01
to
cbbr...@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

Paul Foley

unread,
Oct 31, 2001, 4:37:16 AM10/31/01
to
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>"))

Coby Beck

unread,
Oct 31, 2001, 7:56:24 AM10/31/01
to

<cbbr...@acm.org> wrote in message
news:G5MD7.27208$6h5.1...@news20.bellglobal.com...

> "Coby Beck" <cb...@mercury.bc.ca> writes:
> > <cbbr...@cbbrowne.com> wrote in message
> > news:dnLD7.27076$6h5.1...@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.

cbbr...@acm.org

unread,
Oct 31, 2001, 9:25:32 AM10/31/01
to

I quite entirely realize that; the point was to take into
consideration as many spurious specification elements as possible...


--
(reverse (concatenate 'string "gro.gultn@" "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/>

Lieven Marchand

unread,
Oct 31, 2001, 1:37:41 PM10/31/01
to
cbbr...@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 <m...@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

Raymond Toy

unread,
Oct 31, 2001, 3:52:40 PM10/31/01
to
>>>>> "Lieven" == Lieven Marchand <m...@wyrd.be> writes:

Lieven> cbbr...@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

cbbr...@acm.org

unread,
Oct 31, 2001, 4:37:06 PM10/31/01
to
Lieven Marchand <m...@wyrd.be> writes:
> cbbr...@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.

Paul Foley

unread,
Oct 31, 2001, 6:51:47 PM10/31/01
to

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

David Hanley

unread,
Nov 1, 2001, 7:19:00 PM11/1/01
to
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

Daniel Barlow

unread,
Nov 2, 2001, 9:26:00 AM11/2/01
to
david...@yahoo.com (David Hanley) writes:

Shouldn't this all be in UPPERCASE?


-dan

--

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

0 new messages