Page 1 of 1
((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to
Posted: Tue Nov 03, 2009 10:07 pm
by TedWalther
What is the fastest/cleanest way to do this in newlisp?
Code: Select all
((a b c) (1 2 3)) => ((a 1) (b 2) (c 3))
I'd like to convert some parallel lists to association lists. I know I can do it with a simple doloop. I just have a nagging feeling that there has to be a simpler, more built-in way to do it.
Also, I find it annoying that newlisp symbols aren't required to be separated by brackets or spaces. I'd like to do this:
And I chose those forms because I understand that implicit indexing makes the following two forms impossible:
Ted
Re: ((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to
Posted: Tue Nov 03, 2009 11:04 pm
by Kazimir Majorinc
Perhaps this one.
(transpose '((a b c)(1 2 3))) => ((a 1) (b 2) (c 3))
I didn't quite understood the second note.
Re: ((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to
Posted: Wed Nov 04, 2009 12:58 am
by TedWalther
Thank you Kazimir! I had a hunch there was something in the matrix stuff that would do it since it is a vector type operation. To give more clarity on the other stuff:
Code: Select all
(1+ x) => (+ x 1)
(1- x) => (- x 1)
That is what I'd like to define as functions. Wonder if the new macro stuff would allow it? This touches on how symbols are parsed in newlisp; 1 and - are counted as separate symbols instead of being considered as one symbol 1-
Also, while I'm at it, it is getting annoying, the fact that inc coerces everything to float. If I am dealing with ints, incrementing an int by an int, how does it make sense to cast it to float?
Why should this yield a float?
Re: ((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to
Posted: Wed Nov 04, 2009 12:44 pm
by Lutz
Making a macro for 1+ or 1- would not work (*), because newLISP's parser will take 1+ as two tokens 1 and +, and in +1 -1 the parser will take the sign as a prefix to the number, but here is a macro for incrementing without coercion to float:
Code: Select all
(macro (+= X Y) (setq X (+ X Y)))
(set 'x 12)
(+= x 2) => 14
(*) you still could make it work by writing a different macro.lsp module, the current one always looks for an undefined identifier has the first token, but you could write that differently.
Re: ((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to
Posted: Wed Nov 04, 2009 7:45 pm
by TedWalther
Thanks Lutz. I guess I'll have to define plus1 and minus1 to do what I want.
Can you enlighten me a little about your rationale for doing parsing the way you are? Is there a reason to not have atoms delimited solely by whitespace and brackets? Was it for allowing code compacting in shipping applications?
Ted
Re: ((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to
Posted: Wed Nov 04, 2009 8:13 pm
by Kazimir Majorinc
You can use Roman numbers.
+I, -I, I+, I-
Define all four so you'll not have to remember which one you defined, +I or I+.