Author Topic: rotate a list  (Read 2896 times)

0 Members and 1 Guest are viewing this topic.

curmudgeon

  • Newt
  • Posts: 194
rotate a list
« on: March 19, 2009, 09:23:10 PM »
I am confused. I have a list of points to process, rotate like a stack in RP. I can
 
(setq foo (append (cdr foo) (list (car foo))) )

but when I try to make a subroutine of it

(defun rot_lst ( / )
  (setq foo (append (cdr foo) (list (car foo))) )
)

old foo is new foo, or setq is not setting q.
the list does not rotate.

??
Never express yourself more clearly than you are able to think.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: rotate a list
« Reply #1 on: March 19, 2009, 09:43:04 PM »
Ha ha, foo you. Sorry, couldn't resist. :D

Code: [Select]
(defun rotate ( lst )
    (append
        (cdr lst)
        (list (car lst))
    )
)

(setq lst '(1 2 3))

(rotate lst) => (2 3 1)

(rotate (rotate lst)) => (3 1 2)

(rotate (rotate (rotate lst))) = > (1 2 3)

/shrug

PS: What's RP?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

curmudgeon

  • Newt
  • Posts: 194
Re: rotate a list
« Reply #2 on: March 19, 2009, 09:50:37 PM »
thanks

RP - reverse polish notation like the old HP calculators use.
Never express yourself more clearly than you are able to think.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2138
  • class keyThumper<T>:ILazy<T>
Re: rotate a list
« Reply #3 on: March 19, 2009, 10:17:12 PM »
thanks

RP - reverse polish notation like the old HP calculators use.

:)   and like the new ones too :)

1 ENTER 1 +
30 ENTER 15 TAN *
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: rotate a list
« Reply #4 on: March 19, 2009, 10:26:43 PM »
Well if ya had said RPN stack I wouldn't have asked. I've owned a 10c, 11c, 42cx, 28s and and a 35s. Weirder yet, earlier today I had suggested to a colleague he code up a RPN calculator in Python. :D
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: rotate a list
« Reply #5 on: March 20, 2009, 12:22:03 AM »
thanks

RP - reverse polish notation like the old HP calculators use.

What do you mean OLD. :-o
I use my HP12C every day. 8-)

PS I know it as RPN
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

curmudgeon

  • Newt
  • Posts: 194
Re: rotate a list
« Reply #6 on: March 20, 2009, 03:50:57 AM »
"Well, I'm back." he said.

I am running Autocad 2000, and vlisp, and it seems that I have no definition for ROTATE.
But I do have a deadline of about 10:30 Central Standard ( daylight savings ) Time.
I have 8 hours. But this is still a fun room.

I can (setq points (append (cdr points) (list (car points)))). It works to do this thing.

My original curiosity was, and is, why when I put that in a (defun ) it doesn't change points permanently when I (setq).
 ((5101.57 2550.21) (5080.36 2571.42) (5046.57 2571.42) (5011.65 2571.42) (4990.44 2550.21) (5037.45 2503.2) (5054.56 2503.2))

would be an example of my points.

When I (append (cdr points) (list (car points))) I get
 ((5080.36 2571.42) (5046.57 2571.42) (5011.65 2571.42) (4990.44 2550.21) (5037.45 2503.2) (5054.56 2503.2) (5101.57 2550.21))
which is nifty - especially sans a ROTATE.

WOW! It is so much nicer to type with colour.

Anyway, I love deadlines. I love the whooshing noise they make as they pass. But I think that as long as I am breathing I will want to know the stuff I don't know, and this nugget is on the list of stuff to comprehend.

thanks,

roy

Never express yourself more clearly than you are able to think.

curmudgeon

  • Newt
  • Posts: 194
Re: rotate a list
« Reply #7 on: March 20, 2009, 03:59:08 AM »
Oh, yeah. RPN. Maybe dropping the "N" was a local thing, I seem to remember saying it both ways in school. And maybe my memory is no better than that.
 :ugly:

(AND (= local_thing explanation) (< memory was_memory))
T
Never express yourself more clearly than you are able to think.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: rotate a list
« Reply #8 on: March 20, 2009, 10:03:20 AM »
"Well, I'm back." he said ...

Well the only thing I can surmise is that the function you posted in your initial thread didn't represent what you were actually using off line.

To be more direct, you posted this:

Code: [Select]
(defun rot_lst ( / )
    (setq foo (append (cdr foo) (list (car foo))))
)

Whereas I'm guessing you were actually using this:

Code: [Select]
(defun rot_lst ( [color=red]foo[/color] )
    (setq foo (append (cdr foo) (list (car foo))))
)

Using the second definition thusly:

(setq lst '(A B C))

(rot_lst lst) => (B C A)

While (rot_lst lst) returns (B C A) the variable lst will still be bound to (A B C). Why? Because the argument foo hosts a copy of the data, not the original -- any changes made to foo are local to the rot_lst function and independent of the external variable lst. So while (rot_lst lst) returns the result you want, said results are not permanent -- unless you capture them:

(setq lst (rot_lst lst))

Then again I've quite likely missed your point, but unfortunately I cannot help myself; sorry + shrug.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

curmudgeon

  • Newt
  • Posts: 194
Re: rotate a list
« Reply #9 on: March 22, 2009, 07:34:04 AM »
thank you very much. I did not really understand my own question, but your answer is the best explanation of what I was misunderstanding I have yet seen.

I was mistaken about this very basic concept from the (my) beginning(s).

by way of explanation, I had been working at a different computer, and did not have the url for the swamp there. it took me time to get back here. I had gone to afralisp.net, and reviewed the difference between set and setq, and I almost had it.

I may take your words and put them on my refrigerator for a while - till it really sinks in.

again, thanks.  :-)

PS
I would like to say that I only know what I have picked up along the way. I have no teaching other than the help files and groups like this one - my own files and my own time. in this "project" I have also learned how I can use (princ) to do more than just close quietly.

I am learning quite a lot on this one. all my code is about to get a lot better.

« Last Edit: March 22, 2009, 08:03:57 AM by curmudgeon »
Never express yourself more clearly than you are able to think.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: rotate a list
« Reply #10 on: March 22, 2009, 09:30:44 AM »
I would like to say that I only know what I have picked up along the way. I have no teaching other than the help files and groups like this one - my own files and my own time.

In other words, you're just like us.

Glad you got the help you wanted and are enjoying your programming, happy ones and zeros ...

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: rotate a list
« Reply #11 on: March 22, 2009, 01:04:44 PM »
I would like to say that I only know what I have picked up along the way. I have no teaching other than the help files and groups like this one - my own files and my own time.

In other words, you're just like us.

Yep, welcome to the club, Roy!