((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to

Q&A's, tips, howto's
Locked
TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to

Post 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:

Code: Select all

(1+ x) 
(1- x)
And I chose those forms because I understand that implicit indexing makes the following two forms impossible:

Code: Select all

(-1 x)
(+1 x)
Ted
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Re: ((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to

Post 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.

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: ((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to

Post 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?

Code: Select all

(setq f 1)
(inc f 2)
=> 3.0
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: ((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to

Post 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.

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: ((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to

Post 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
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Re: ((a b c) (1 2 3)) => ((a 1) (b 2) (c 3)) (parallel lists to

Post 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+.

Locked