unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* list conversion
@ 2003-05-06 19:33 Zhengao. Huang
  0 siblings, 0 replies; 4+ messages in thread
From: Zhengao. Huang @ 2003-05-06 19:33 UTC (permalink / raw)



Given a list ("first" "second" "third"), I'd like to  transfrom it into the
a new list: 
   (("" . "first")
    ("" . "second")
    ("" . "third"))	

The following lisp code was intended for this purpose, but not working. It
looks that I missed some very basic stuff (I rarely code in lisp). Can
anyone point out what is wrong? Thanks in advance. 

(defun list-conversion (arg)
  (let ((l arg) (new-list ()))
    (while l
      (append new-list (list "" . (car l))))
      (setq l (cdr l))))

^ permalink raw reply	[flat|nested] 4+ messages in thread

* list conversion
@ 2003-05-06 19:46 Z. Huang
  2003-05-06 20:04 ` Barry Margolin
  2003-05-06 20:09 ` Jesper Harder
  0 siblings, 2 replies; 4+ messages in thread
From: Z. Huang @ 2003-05-06 19:46 UTC (permalink / raw)



Given a list ("first" "second" "third"). I'd to convert it to a 
a new list: 
   (("" . "first")
    ("" . "second")
    ("" . "third"))	

The following lisp code was intended for this purpose, but not working. It
looks that I missed some very basic stuff (I rarely code in lisp). Can
anyone point out what is wrong? Thanks in advance. 

(defun list-conversion (arg)
  (let ((l arg) (new-list ()))
    (while l
      (append new-list (list "" . (car l))))
      (setq l (cdr l))))

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: list conversion
  2003-05-06 19:46 list conversion Z. Huang
@ 2003-05-06 20:04 ` Barry Margolin
  2003-05-06 20:09 ` Jesper Harder
  1 sibling, 0 replies; 4+ messages in thread
From: Barry Margolin @ 2003-05-06 20:04 UTC (permalink / raw)


In article <yq0cznlz26za.fsf@blinky.bloomberg.com>,
Z. Huang <zghuang@bloomberg.net> wrote:
>(defun list-conversion (arg)
>  (let ((l arg) (new-list ()))
>    (while l
>      (append new-list (list "" . (car l))))
>      (setq l (cdr l))))

(defun list-conversion (list)
  (mapcar '(lambda (item) (cons "" item)) list))

Here's a version more like your original attempt:

(defun list-conversion (list)
  (let ((l arg) (new-list '()))
    (while l
      (setq new-list (cons (cons "" (car l))))
      (setq l (cdr l)))
    (nreverse new-list)))

Note that you need to re-assign new-list each time through the loop; append
creates a new list, but it doesn't change what new-list refers to.  Also,
you need to use cons, not list, to create something of the form (x . y).

-- 
Barry Margolin, barry.margolin@level3.com
Genuity Managed Services, a Level(3) Company, 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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: list conversion
  2003-05-06 19:46 list conversion Z. Huang
  2003-05-06 20:04 ` Barry Margolin
@ 2003-05-06 20:09 ` Jesper Harder
  1 sibling, 0 replies; 4+ messages in thread
From: Jesper Harder @ 2003-05-06 20:09 UTC (permalink / raw)


zghuang@bloomberg.net (Z. Huang) writes:

> Given a list ("first" "second" "third"). I'd to convert it to a 
> a new list: 
>    (("" . "first")
>     ("" . "second")
>     ("" . "third"))	
>
> The following lisp code was intended for this purpose, but not working. It
> looks that I missed some very basic stuff (I rarely code in lisp). Can
> anyone point out what is wrong? Thanks in advance. 

You could do it like this:

(defun list-conversion (list)
  (mapcar (lambda (x) (cons "" x)) list))

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-05-06 20:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-06 19:46 list conversion Z. Huang
2003-05-06 20:04 ` Barry Margolin
2003-05-06 20:09 ` Jesper Harder
  -- strict thread matches above, loose matches on Subject: below --
2003-05-06 19:33 Zhengao. Huang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).