DGRL Posted April 4, 2018 Posted April 4, 2018 Dear coders While playing with the mapcar function i was wondering if the next is possible ill have a list containing multiple lists i.e. (("ent1" " ent2" "ent3")("ent11 "ent22" "ent33")) and now i want to combine them in this (("ent1" "ent11")("Ent2" "ent22")("ent3" "ent23")) Now this is pretty easy to do but now what i really want to try We take the same list but the list comes with an unknown number of sub-lists In my example i took only 2 sub-lists but since i read from txt it can be 10 or 100 sub-lists or any number in between So we start (setq list1 (mapcar 'list (car '(("ent1" " ent2" "ent3")("ent11" "ent22" "ent33")))(cadr '(( "ent1" " ent2" "ent3")("ent11" "ent22" "ent33"))))) How to write this when you dont know the number of sub-lists? i.e (setq list1 (mapcar 'list (foreach x list ....... or (setq list1 (mapcar 'list (lambda ....... Quote
Jef! Posted April 4, 2018 Posted April 4, 2018 Hi DroiteGaucheRightLeft! Nothing really fancy, no error trapping (it would get exponentially more complex if you want to deal with uneven sub lists, depending on how it would need to be processed)... here'S an example on how to achieve that. (defun test (lst / x lst ret) (setq x 0) (repeat (length (car lst)) (foreach itm lst (setq ret (cons (nth x itm) ret))) (setq x (1+ x)) ) (reverse ret) ) Feed the list to the function as follow... Command: (test '(("ent1" " ent2" "ent3")("ent11" "ent22" "ent33")("ent111" "ent222" "ent333")))("ent1" "ent11" "ent111" " ent2" "ent22" "ent222" "ent3" "ent33" "ent333") Cheers! Quote
Lee Mac Posted April 4, 2018 Posted April 4, 2018 (apply 'mapcar (cons 'list <your-list>)) _$ (apply 'mapcar (cons 'list '(("ent1" " ent2" "ent3")("ent11" "ent22" "ent33")("ent111" "ent222" "ent333")))) (("ent1" "ent11" "ent111") (" ent2" "ent22" "ent222") ("ent3" "ent33" "ent333")) Quote
Jef! Posted April 4, 2018 Posted April 4, 2018 Doop! I misread the format of the returned value, a list of list instead of just a list. Oh well. (defun test (lst / x lst ret ret2) (setq x 0) (repeat (length (car lst)) (foreach itm lst (setq ret (cons (nth x itm) ret))) (setq x (1+ x)) (setq ret2 (cons (reverse ret) ret2)) (setq ret nil) ) (reverse ret2) ) Command: (test '(("ent1" " ent2" "ent3")("ent11" "ent22" "ent33")("ent111" "ent222" "ent333")))(("ent1" "ent11" "ent111") (" ent2" "ent22" "ent222") ("ent3" "ent33" "ent333")) Why use simple when you can use complicated? (nicely done Lee!) 1 Quote
DGRL Posted April 4, 2018 Author Posted April 4, 2018 Hi @Jef! Nice one with the name lol ANd for now i want to deal with even numbered sub lists the rest will come later Thanks for both the examples you never know when i need to first one you provided @Lee Mac. Short but very powerfull :-) Quote
rlx Posted April 4, 2018 Posted April 4, 2018 Lee is out of this world (and I think I know where he's from : Gallifrey ) unfortunately couldn't do any better than this meself... (mapcar 'list '(1 2 3) '(11 12 13) '(21 23 23)) Lee , when or if you don't need or use it any more , can I have your brain please gr. Rlx 1 Quote
Grrr Posted April 4, 2018 Posted April 4, 2018 Lee , when or if you don't need or use it any more , can I have your brain please gr. Rlx That sounded like a dark humor, might be a good commercial for organ donation. Previously I was investigating this and thought that the algorithm that guy used seems a bit complex (BTW reminded me of Jef's), so I wondered how hard it would be to do it with LISP.The answer was: not hard at all - (defun RotateMatrix ( mat ) (setq mat (mapcar 'reverse (apply 'mapcar (cons 'list mat)))) (foreach x mat (print x)) ) Which rotates it like in the video: (RotateMatrix '( (0 1) (2 3) ) ) >> (2 0) (3 1) (RotateMatrix '( (0 1 2) (3 4 5) (6 7 ) ) >> (6 3 0) (7 4 1) (8 5 2) (RotateMatrix '( (0 1 2 3) (4 5 6 7) (8 9 10 11) (12 13 14 15) ) ) >> (12 8 4 0) (13 9 5 1) (14 10 6 2) (15 11 7 3) And then I've remembered that this technique (to me) was demonstrated by Lee, and hes mathematician aswell - so when I connected the dots •---• I've left with the impression that in the very first he decided to learn LISP to deal easier with math and not that much with our problems. Quote
rlx Posted April 4, 2018 Posted April 4, 2018 That sounded like a dark humor, might be a good commercial for organ donation. And then I've remembered that this technique (to me) was demonstrated by Lee, and hes mathematician aswell - so when I connected the dots •---• I've left with the impression that in the very first he decided to learn LISP to deal easier with math and not that much with our problems. now look who's talking dark humor here whaha... gr. Rlx Quote
Lee Mac Posted April 4, 2018 Posted April 4, 2018 Thank you all for your kind & flattering compliments. @Grrr, linked-lists (on which LISP is built) and their inherent flexibility by virtue of how they are stored in memory (when compared with the relative rigidity of an array) lend themselves very well to complicated manipulation which would otherwise be relatively difficult to implement through exchanging elements in an array. FWIW, there are a few ways you could write your 'rotate matrix' function, with both left & right rotations - (defun rmat ( m ) (mapcar 'reverse (apply 'mapcar (cons 'list m))) ) (defun rmat2 ( m ) (apply 'mapcar (cons 'list (reverse m))) ) (defun lmat ( m ) (apply 'mapcar (cons 'list (mapcar 'reverse m))) ) (defun lmat2 ( m ) (reverse (apply 'mapcar (cons 'list m))) ) Quote
Grrr Posted April 4, 2018 Posted April 4, 2018 @Grrr, linked-lists (on which LISP is built) and their inherent flexibility by virtue of how they are stored in memory (when compared with the relative rigidity of an array) lend themselves very well to complicated manipulation which would otherwise be relatively difficult to implement through exchanging elements in an array. With the time I realised how easy it is to manipulate data with LISP, and now I completely understand the struggle to switch to another programming language (it just doesn't feel that flexible). Few threads ago mentioned to Rlx about the advantage LISP has. But I still think it would be a good practice to learn more 'common' processing like Jef! offered, due the eventual language switch. FWIW, there are a few ways you could write your 'rotate matrix' function, with both left & right rotations - Thanks Lee, I dug up that function from my archive (was trying to figure out something fun with it in conjuction with grread). Offtopic: ATM the left side of my brain only came up with the idea to rotate some coordinates, which translated to another idea to use your 2D projection + grread in order to 'tilt' the projection/perspective Up/Down/Left/Right with [W/A/S/D] keys. Just imagine it, by looking at your Example #1 there (it looks so cool in my head). The functions you offer might be handy to others aswell especially to DGRL (judging by his interests, I see he has a potential to become 'one of us'). DGRL, note that Lee is (best?/one of the best) List Manipulators out there, so the puns we do are not accidental. Quote
rlx Posted April 5, 2018 Posted April 5, 2018 DGRL, note that Lee is (best?/one of the best) List Manipulators out there, so the puns we do are not accidental. Lets eat errors & Make awesome code :-) Quote
DGRL Posted April 5, 2018 Author Posted April 5, 2018 The functions you offer might be handy to others aswell especially to DGRL (judging by his interests, I see he has a potential to become 'one of us'). DGRL, note that Lee is (best?/one of the best) List Manipulators out there, so the puns we do are not accidental. It would be nice if I can become 'one of you' guys I know there is still a lot to learn and I really wanna thanks each and every 1 of you for helping out and pushing me into right direction. I remember myself somehere here mentioned about seeing AutoCAD as DATABASE and so I did After that a lot of ?? became lightbulbs lol (not that I saw the light xD) And after all I hope to find a job as software engineer ( still a long way to go before I reach that level ) Quote
Jef! Posted April 5, 2018 Posted April 5, 2018 It would be nice if I can become 'one of you' guys I think that anyone with good amount of practice can get to achieve what he needs - like with my example offered, which is less elegant than Lee's... but some will have better in depth understanding of some things based on their individual background and inner wiring. I remember when Grrr just started lisping, I shown him how to process selection set. He reffered to me as a SelSet processing god, but nowadays Grrr is able to play with reactors which is something I never managed to get a grasp on. I'm very visual oriented, maybe that is why; when we use functions we can visualize what they return, as for reactors, we just don't get visual clues. By seeing the different steps of evaluation of a function is what makes me learn. What blew my mind in Lee's approach and just made some lights turn on in my head (apply 'mapcar (cons 'list '[color="blue"](("ent1" " ent2" "ent3")("ent11" "ent22" "ent33")("ent111" "ent222" "ent333"))[/color])) is that in the following part (cons 'list '(("ent1" " ent2" "ent3")("ent11" "ent22" "ent33")("ent111" "ent222" "ent333"))) it shoves the LIST word inside the original list as a car which returns (LIST ("ent1" " ent2" "ent3") ("ent11" "ent22" "ent33") ("ent111" "ent222" "ent333")) hence enabling the use of the apply mapcar on the LIST as a function. (I must admit that I fail to really understand why the apply is required, and why it won'T work in that specific case if the apply is not there. My guess is it is because the cons list part has to be evaluated first? Why it works is for me very fuzzy. ^^). I might have been able to create that solution, but it would have been by chance guided by trial and errors rather that by making it right away guided by control and understanding.But I still think it would be a good practice to learn more 'common' processing like Jef! offered, due the eventual language switch. Whenever I make functions, I make "common" processing to get base line and also help me "visualize" what needs to be achieved, the different steps, then try to refine and/or make use of more complex but concise functions using mapcar/lambda/ to "automate" the steps. Usually for each processing I need I write between 2 to 4 functions that I then benchmark for performance to determine which one I end up using. Benchmark is by far the best teacher when someone is interested in learning about performance. I still feel like I just learned to walk not long ago, and while I manage to keep my balance Lee is like Usain Bolt running around me Quote
Grrr Posted April 5, 2018 Posted April 5, 2018 I remember when Grrr just started lisping, I shown him how to process selection set. He reffered to me as a SelSet processing god, I remember that too, you provided quite lengthy answers there. Actually I remember everyone's help here on this forum (mostly from Lee Mac and Tharwat, aswell BIGAL's and Roy's). BIGAL taught me of the progn function, Rlx once created a thread to show one of his programs.. and when I tried it I got some error, so he started explaining how to debbug it. And I've lost count on how many times Lee and Tharwat helped me. I still don't claim that I know everything, so I might come up with 'silly questions or discoveries' again. but nowadays Grrr is able to play with reactors which is something I never managed to get a grasp on. Reactors are not that scary, once you get the handle of it.. I even don't understand how some 'starting lispers' decide to deal with reactors, that have alot less common lisp-coding knowledge than you. (Look for a user 'AfterTouch') They are just a different aspect of LISP. What blew my mind in Lee's approach and just made some lights turn on in my head (apply 'mapcar (cons 'list '[color="blue"](("ent1" " ent2" "ent3")("ent11" "ent22" "ent33")("ent111" "ent222" "ent333"))[/color])) is that in the following part (cons 'list '(("ent1" " ent2" "ent3")("ent11" "ent22" "ent33")("ent111" "ent222" "ent333"))) it shoves the LIST word inside the original list as a car which returns hence enabling the use of the apply mapcar on the LIST as a function. (I must admit that I fail to really understand why the apply is required, and why it won'T work in that specific case if the apply is not there. Looks like you made a 'discovery' from Lee, although its not the first time he revealed this technique.. Here he introduced me to it. Here he describes whats happening. Here he shoves up a lambda function in order to find a mid point, from (list p1 p2). Example 6 from his mapcar 'n' lambda tutorial. I still feel like I just learned to walk not long ago, and while I manage to keep my balance Lee is like Usain Bolt running around me We all learn, (I'm still learning stuff from you guys - recently from Rlx's and RJP's codes). The key is to keep a low ego and just ask.. I don't give a damn if I have to create another thread like "how to use progn?". Quote
Lee Mac Posted April 5, 2018 Posted April 5, 2018 Looks like you made a 'discovery' from Lee, although its not the first time he revealed this technique..Here he introduced me to it. Here he describes whats happening. Here he shoves up a lambda function in order to find a mid point, from (list p1 p2). Example 6 from his mapcar 'n' lambda tutorial. Well catalogued! Quote
Grrr Posted April 5, 2018 Posted April 5, 2018 Well catalogued! Thanks Lee, thats the least thing I could do from your help. Quote
rlx Posted April 5, 2018 Posted April 5, 2018 Thanks Lee, thats the least thing I could do from your help. Very helpfull , thank you both ! Lee for making it and Grrr for stealing it haha. when I don't get something I do a digital biopsy , something like : defun tst ( / l1 l2 l3) (setq l1 '(1 2 3) l2 '(11 12 13) l3 '(21 22 23) l4 (list l1 l2 l3)) (apply 'list l1) ; -> (1 2 3) (apply 'list (list l1 l2 l3)) ; -> ((1 2 3) (11 12 13) (21 22 23)) (apply 'mapcar (list l1 l2 l3)) ; -> error: bad function: 1 (apply '+ l1) ; -> 6 (apply '+ (list l1 l2 l3)) ; -> error: bad argument type: numberp: (1 2 3) (apply 'mapcar (cons '+ l4)) ; -> (33 36 39) (apply 'mapcar (cons 'list l4)) ; -> ((1 11 21) (2 12 22) (3 13 23)) ) (tst) and usually then I go like ahhhhh , now the biting makes sense! Quote
hanhphuc Posted April 6, 2018 Posted April 6, 2018 i recall this was explained by Lee too (apply 'mapcar (cons 'list l4)) (mapcar 'list l1 l2 l3 ) ;; -> ((1 11 21) (2 12 22) (3 13 23)) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.