handasa Posted June 27, 2017 Posted June 27, 2017 Greetings everyone ... i have a list consist of sublists ( (B1 B2) (B3 B4) (B5 B6) ) i need a way to merge two sublists of them say: (B1 B2)(B3 B4) together to be (B1 B2 B3 B4) AND THE FINAL MASTER LIST WILL LOOKS LIKE ( (B1 B2 B3 B4) (B5 B6) ) the functions should supplied by the "nth" position of the two sublists which i need to merge... thanks and best regards Quote
Lee Mac Posted June 27, 2017 Posted June 27, 2017 Assuming that the result should appear at the highest index: (defun mergesublists ( idx lst / i l m ) (setq m (apply 'max idx) i -1 ) (vl-remove nil (mapcar '(lambda ( x ) (if (member (setq i (1+ i)) idx) (progn (setq l (append l x)) (if (= m i) l)) x ) ) lst ) ) ) _$ (setq lst '((B1 B2)(B3 B4)(B5 B6))) ((B1 B2) (B3 B4) (B5 B6)) _$ (mergesublists '(0 1) lst) ((B1 B2 B3 B4) (B5 B6)) _$ (mergesublists '(0 2) lst) ((B3 B4) (B1 B2 B5 B6)) _$ (mergesublists '(1 2) lst) ((B1 B2) (B3 B4 B5 B6)) Quote
handasa Posted June 27, 2017 Author Posted June 27, 2017 works fine .... thanks Mr. lee i need a further more advice to fulfill my project please ... i can't remove an entire sublist such (B1 B2) from a master list contains other sublists '( (B1 B2) (B3 B4) (B5 B6) ) using (vl-remove (nth 0 lst) lst) doesn't work for me ... any suggest ? and thanks in advance Quote
Jef! Posted June 28, 2017 Posted June 28, 2017 Handasa, the way you wrote it is correct. this will return the list without the element (and every occurances of the element too, be carefull). Look in my example how it removes both 0. Command: (setq lst '(0 3 4 2 0 6 )(0 3 4 2 0 6 Command: (vl-remove (nth 0 lst) lst) (3 4 2 6 One other thing: it returns the list without the element(s), but you didn't redefine (setq) lst with the result, which may be why you say it doesn't work. (setq lst '(0 3 4 2 0 6 )(0 3 4 2 0 6 Command: (vl-remove (nth 0 lst) lst) (3 4 2 6 Command: !lst (0 3 4 2 0 6 Command: (setq lst (vl-remove (nth 0 lst) lst)) (3 4 2 6 Command: !lst (3 4 2 6 Quote
handasa Posted June 28, 2017 Author Posted June 28, 2017 Handasa, the way you wrote it is correct. this will return the list without the element (and every occurances of the element too, be carefull). Look in my example how it removes both 0. One other thing: it returns the list without the element(s), but you didn't redefine (setq) lst with the result, which may be why you say it doesn't work. Greetings , jef ... i checked it and it worked for me this time ... may be i was writing the code improperly ... thanks jef Quote
Grrr Posted June 28, 2017 Posted June 28, 2017 Assuming that the result should appear at the highest index : Lee, why not use a boolean? BTW considering the amount of such subfunctions you are posting - you are damn impressive list manipulator! Quote
Lee Mac Posted June 28, 2017 Posted June 28, 2017 Lee, why not use a boolean? BTW considering the amount of such subfunctions you are posting - you are damn impressive list manipulator! Thank you Grrr - but where are you suggesting the use of the boolean? In what way? Quote
Grrr Posted June 28, 2017 Posted June 28, 2017 Thank you Grrr - but where are you suggesting the use of the boolean? In what way? So the result could appear at the lowest/highest index, depending on it - i.e.: _$ (setq lst '((B1 B2)(B3 B4)(B5 B6))) ((B1 B2) (B3 B4) (B5 B6)) _$ (mergesublists '(0 2) lst nil) ((B3 B4) (B1 B2 B5 B6)) _$ (mergesublists '(0 2) lst T) ((B1 B2 B5 B6) (B3 B4)) But after rethinking, the user might supply list for the "idx" argument like: '(0 5 8 9) So my suggestion might be not good. 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.