Author Topic: Replace item in list with new item  (Read 7688 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
Re: Replace item in list with new item
« Reply #15 on: February 13, 2008, 02:11:12 PM »
Hmm. First off, I guess I don't understand why you're using eval or what eval is doing here? I set up a list like: (setq llist '(1 2 3 4 5)) and ran your function and it crashed. On deeper inspection, the eval crashed right out of the gate. What should I be supplying here?

JohnK

  • Administrator
  • Seagull
  • Posts: 10652
Re: Replace item in list with new item
« Reply #16 on: February 13, 2008, 02:40:15 PM »
Im using eval because...read up on ``set''.

Did you read the comments?

(setq llist '(1 2 3 4 5))
(list-pop 'llist)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

daron

  • Guest
Re: Replace item in list with new item
« Reply #17 on: February 13, 2008, 03:09:51 PM »
Yes I read the comments. I think my mind is turning to mush. Let me show you what I've been trying to accomplish and see if you can lead me to understanding what I'm not getting.
Code: [Select]
(defun pop (masterList pos)
     (setq inc -1)
     (set 'tval (nth pos masterList))
     (setq old       (mapcar '(lambda (x)
    (setq inc (1+ inc))
    (cons inc x)
       )
      masterList
      )
   new       (subst nil (assoc pos old) old)
   masterList (vl-remove 'nil (append (mapcar 'cdr new)))
     )
     (list tval masterList)
)
After forgetting what eval and set are really doing for you, I wasn't getting the return value properly. What this is trying to do is improve on your initial idea. It would be cool to have the ability to remove an item from anywhere within the list and alter the list like you've done. Somewhere in there I need to add an eval. I'm just not sure where now. As for why eval crashed on me, I figured it out.

JohnK

  • Administrator
  • Seagull
  • Posts: 10652
Re: Replace item in list with new item
« Reply #18 on: February 13, 2008, 05:01:12 PM »
Sorry man but your code is a bit confusing.

Here is a quick and dirty version for removing position using mapcar (I think it might be easier with a loop but none the less.).

Code: [Select]
(defun positional-list-pop (lst pos / ~tval cntr)
  (setq ~tval (eval lst)
        cntr -1)
  (set lst
       (mapcar
         '(lambda ( x )
            (setq cntr (1+ cntr))
            (if (not (eq cntr pos)) x))
         ~tval
         )
       )
  pos
  )

(setq llist '(1 2 3 4 2 2 5))
(positional-list-pop 'llist 2)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Replace item in list with new item
« Reply #19 on: February 13, 2008, 05:10:01 PM »
That just places a 'nil' in place of the value.  I don't think you can use 'mapcar' for this, at least I haven't seen a way and can't think of one without using a 'vl-remove....' something.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

JohnK

  • Administrator
  • Seagull
  • Posts: 10652
Re: Replace item in list with new item
« Reply #20 on: February 13, 2008, 05:40:01 PM »
yep.

I was thinking a loop would be better.

Let me think about it some more.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Replace item in list with new item
« Reply #21 on: February 13, 2008, 05:47:15 PM »
Code: [Select]
(defun RemoveNth (InList Position / OutList)
  (while (> Position 0)
    (setq OutList  (cons (car InList) OutList)
  InList   (cdr InList)
  Position (1- Position)
    )
  )
  (append (reverse OutList) (cdr InList))
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10652
Re: Replace item in list with new item
« Reply #22 on: February 13, 2008, 05:51:34 PM »
VovKa is using a loop like what i would have done. Although, that procedure does not redefine the orig variable, that is just a small fix.

Seeing as i beet to the punch; We can take a play from Matt Stachoni's book and do this as well?

Code: [Select]
(defun positional-list-pop (lst pos / ~tval cntr)
  (setq ~tval (eval lst)
        ~tval
        (apply 
          'append
          (subst nil (list (nth pos ~tval)) (mapcar 'list ~tval))))
  (set lst ~tval)
  pos
  )

(setq llist '(1 2 3 4 2 2 5))
(positional-list-pop 'llist 2)

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Replace item in list with new item
« Reply #23 on: February 13, 2008, 06:10:14 PM »
Code: [Select]
(setq llist '(3 1 2 3 4 2 2))
(positional-list-pop 'llist 2)
llist would be '(3 1 3 4), and that is wrong. subst is not good for this

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Replace item in list with new item
« Reply #24 on: February 13, 2008, 06:16:36 PM »
Code: [Select]
(setq llist '(3 1 2 3 4 2 2))
(positional-list-pop 'llist 2)
llist would be '(3 1 3 4), and that is wrong. subst is not good for this
Good catch VovKa.
Quote from: Acad help for 'subst'
Searches a list for an old item and returns a copy of the list with a new item substituted in place of every occurrence of the old item
Never read this before.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

JohnK

  • Administrator
  • Seagull
  • Posts: 10652
Re: Replace item in list with new item
« Reply #25 on: February 13, 2008, 06:34:47 PM »
Ah shoot! ...Fine, be that way! Not a loop in there!
(Have fun with a big list *ptttht!*)

Code: [Select]
(defun positional-list-pop (lst pos / ~tval cntr)
  (setq ~tval (eval lst)
cntr -1
~tval
      (apply
'append
(subst nil
       (list nil)
       (mapcar 'list
       (mapcar
'(lambda (x)
    (setq cntr (1+ cntr))
    (if (not (eq cntr pos))
      x
    )
  )
~tval
       )
       )
)
      )
  )
  (set lst ~tval)
  pos
)


Preforming this operation without using a loop is getting ridiculous! What are we on four n{fx4} now?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Replace item in list with new item
« Reply #26 on: February 13, 2008, 06:39:53 PM »
no loops, no recursions, no speed :)
Code: [Select]
(defun RemoveNth3 (InList Position /)
  ((lambda (rest)
     (append (reverse
       (cdr
(vl-member-if
   (function (lambda (e) (= (setq Position (1+ Position)) (length rest)))
   )
   (reverse InList)
)
       )
     )
     rest
     )
   )
    (cdr (vl-member-if
   (function (lambda (e) (< (setq Position (1- Position)) 0)))
   InList
)
    )
  )
)

daron

  • Guest
Re: Replace item in list with new item
« Reply #27 on: February 13, 2008, 06:43:30 PM »
well, this has been a fun challenge. I'll have to check these things out later.

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Replace item in list with new item
« Reply #28 on: February 13, 2008, 06:49:39 PM »
and the easiest one
Code: [Select]
(defun RemoveNth4 (InList Position /)
  (vl-remove-if
    (function (lambda (e) (= (setq Position (1- Position)) -1)))
    InList
  )
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10652
Re: Replace item in list with new item
« Reply #29 on: February 13, 2008, 06:52:52 PM »
no loops, no recursions, no speed :)

yeah, but he wanted a lisp pop procedure AND use mapcar. :P
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org