Author Topic: Exercise in List Manipulation ~ Bring Element to Front of List  (Read 4962 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Exercise in List Manipulation ~ Bring Element to Front of List
« Reply #15 on: September 13, 2009, 11:44:35 AM »
ArgV, the member function does not take an index argument  :wink:

Also, be careful with vl-remove - it will remove all instances...

Oh, 1 more thing :-)  No need for the progn - you are only using 1 "then" statment :wink:
« Last Edit: September 13, 2009, 12:44:56 PM by Lee Mac »

ArgV

  • Guest
Re: Exercise in List Manipulation ~ Bring Element to Front of List
« Reply #16 on: September 13, 2009, 02:47:29 PM »
ArgV, the member function does not take an index argument  :wink:

Also, be careful with vl-remove - it will remove all instances...

Oh, 1 more thing :-)  No need for the progn - you are only using 1 "then" statment :wink:

Well crap. I guess I misunderstood the task. There are many ways to do something like that obviously.. just depends on what it's for, or how it's used I guess. ?

So what is the ULTIMATE goal of this routine? Is it just for integers, or other data types, or Multiple data types? will there be more than one of the same symbol in the list? What then?

 :?

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Exercise in List Manipulation ~ Bring Element to Front of List
« Reply #17 on: September 13, 2009, 02:54:15 PM »
ArgV, the member function does not take an index argument  :wink:

Also, be careful with vl-remove - it will remove all instances...

Oh, 1 more thing :-)  No need for the progn - you are only using 1 "then" statment :wink:

Well crap. I guess I misunderstood the task. There are many ways to do something like that obviously.. just depends on what it's for, or how it's used I guess. ?

So what is the ULTIMATE goal of this routine? Is it just for integers, or other data types, or Multiple data types? will there be more than one of the same symbol in the list? What then?

 :?

That shouldn't make a difference since we're moving the items based on location, not values.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Exercise in List Manipulation ~ Bring Element to Front of List
« Reply #18 on: September 13, 2009, 02:55:43 PM »
The task was just to take a list and move the nth element in that list to the front - nothing more to it.

Your problem fell down not by the misinterpretation of the task at hand, but through your use of the num argument  ;-)

ArgV

  • Guest
Re: Exercise in List Manipulation ~ Bring Element to Front of List
« Reply #19 on: September 13, 2009, 03:04:44 PM »
The task was just to take a list and move the nth element in that list to the front - nothing more to it.

Your problem fell down not by the misinterpretation of the task at hand, but through your use of the num argument  ;-)

Hmm.. ok. Well, I learned some things from the thread, so I guess thats all that matters. I still have trouble relating arrays to lists, but I'm getting kinda better. :) I took this challenge because I need to get better at working with lists, so thank you for it. :)

« Last Edit: September 13, 2009, 03:47:03 PM by ArgV »

ArgV

  • Guest
Re: Exercise in List Manipulation ~ Bring Element to Front of List
« Reply #20 on: September 13, 2009, 04:12:02 PM »
The task was just to take a list and move the nth element in that list to the front - nothing more to it.

Your problem fell down not by the misinterpretation of the task at hand, but through your use of the num argument  ;-)

This?

Code: [Select]
(defun ToFrontArgv (offset lst / offset )
 
(setq lst (append (list (nth offset lst)) lst)
      lst (acet-list-remove-nth (1+ (nth offset lst)) lst))
  )

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Exercise in List Manipulation ~ Bring Element to Front of List
« Reply #21 on: September 13, 2009, 04:23:54 PM »
The task was just to take a list and move the nth element in that list to the front - nothing more to it.

Your problem fell down not by the misinterpretation of the task at hand, but through your use of the num argument  ;-)

This?

Code: [Select]
(defun ToFrontArgv (offset lst / offset )
  
(setq lst (append (list (nth offset lst)) lst)
      lst (acet-list-remove-nth (1+ (nth offset lst)) lst))
  )


Problem is, you require express tools to exist and be loaded. However, you could make it a lot easier like this:
Code: [Select]
(defun ToFront (n L)
  (if (zerop n)
    L
    (cons (nth n L) (acet-list-remove-nth n L))
    ))
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

ArgV

  • Guest
Re: Exercise in List Manipulation ~ Bring Element to Front of List
« Reply #22 on: September 13, 2009, 04:28:43 PM »
The task was just to take a list and move the nth element in that list to the front - nothing more to it.

Your problem fell down not by the misinterpretation of the task at hand, but through your use of the num argument  ;-)

This?

Code: [Select]
(defun ToFrontArgv (offset lst / offset )
  
(setq lst (append (list (nth offset lst)) lst)
      lst (acet-list-remove-nth (1+ (nth offset lst)) lst))
  )


Problem is, you require express tools to exist and be loaded. However, you could make it a lot easier like this:
Code: [Select]
(defun ToFront (n L)
  (if (zerop n)
    L
    (cons (nth n L) (acet-list-remove-nth n L))
    ))

Ok, cool. well, thats pretty cool. I just found that command and tried it. I just thought i'd see what I could come up with. Nothing compared to the wizards here. :) Thats why I'm here... to learn.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Exercise in List Manipulation ~ Bring Element to Front of List
« Reply #23 on: September 13, 2009, 04:42:51 PM »
Check my post first for a "non-Express" version of the Remove_nth function  ;-)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Exercise in List Manipulation ~ Bring Element to Front of List
« Reply #24 on: September 13, 2009, 05:04:20 PM »
Check my post first for a "non-Express" version of the Remove_nth function  ;-)
lol
I didn't pay any attention to it at first, but I just noticed that I wrote the exact same function, the exact same way in June.
Code: [Select]
(defun AT:NthRemove (#Nth #List / #Index)
  (setq #Index -1)
  (vl-remove-if
    '(lambda (x) (eq #Nth (setq #Index (1+ #Index))))
    #List
  ) ;_ vl-remove-if
) ;_ defun

The only difference, is the variable naming scheme (I'm a little verbose in mine).
Funny how that works.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox