Kowal Posted October 12, 2014 Share Posted October 12, 2014 Hi, How to merge two lists into one? The first list is one piece and the resulting list is her equal to the number of elements. The first list: ‘(10.3403 10.3417 10.3431 …) The second list ‘((10.3406333333333 22.2839283048) (10.3407111111111 22.2900230856) (10.3407888888889 22.3601283048) (10.3409444444444 22.3936563048) (10.3408666666667 22.3997513904) (10.3410222222222 22.457664) (10.3411 22.457664) (10.3413333333333 22.4149923048) (10.3412555555556 22.424136) (10.3411777777778 22.4515692192) (10.3414888888889 22.2991692192) (10.3415666666667 22.3205049144) (10.3416444444444 22.3235513904) (10.3414111111111 22.3448873904) (10.3418 22.3144076952) (10.3417222222222 22.3205049144) (10.3418777777778 22.4058486096) (10.3421888888889 22.4088950856) (10.3421111111111 22.4485206096) (10.3419555555556 22.4515692192) (10.3420333333333 22.479) (10.3423444444444 22.3418409144) (10.3422666666667 22.3784153904) (10.3424222222222 22.3997513904) (10.3427333333333 22.1711513904) (10.3426555555556 22.2138230856) (10.3425777777778 22.305264) (10.3425 22.3601283048) (10.3430444444444 22.0096076952)) List of results: ‘((10.3403 10.3406333333333 22.2839283048) (10.3417 10.3416444444444 22.3235513904) (10.3431 10.3430444444444 22.0096076952)) Algorithm: Select an item from the first list: (mapcar '(lambda (a) (…) lst1)) Subtract the value of each element sublisty: (mapcar '(lambda (e) (abs (- a (car e)))) lst2) We choose the smallest value (interval): (apply 'min (mapcar '(lambda (e) (- a (car e))) lst2)) How to put this? Sorry for my english. Quote Link to comment Share on other sites More sharing options...
marko_ribar Posted October 12, 2014 Share Posted October 12, 2014 (defun mininterval ( lst1 lst2 ) (car (vl-sort (mapcar '(lambda ( a b ) (abs (- a (car b)))) lst1 lst2) '(lambda ( a b ) (< a b)))) ) Quote Link to comment Share on other sites More sharing options...
hanhphuc Posted October 12, 2014 Share Posted October 12, 2014 Trying guru Doug Wilson's famous method (vl-sort (apply 'mapcar (cons 'cons (list lst1 lst2))); just did a tweak ''(( a b ) (< (car a) (car b)))) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 12, 2014 Share Posted October 12, 2014 Trying guru Doug Wilson's famous method (vl-sort (apply 'mapcar (cons 'cons (list lst1 lst2))); just did a tweak ''(( a b ) (< (car a) (car b)))) hanhphuc, note that: (apply 'mapcar (cons 'cons (list lst1 lst2))) Is the same as: (mapcar 'cons lst1 lst2) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 12, 2014 Share Posted October 12, 2014 I could be wrong, but I understood the OP was looking for something like this: (defun f ( l1 l2 ) (setq l1 (vl-sort l1 '<) l2 (vl-sort l2 '(lambda ( a b ) (< (car a) (car b)))) ) (mapcar '(lambda ( a ) (cons a (cond ( (vl-some '(lambda ( b c ) (if (< a (car c)) (if (< (abs (- a (car c))) (abs (- a (car b)))) c b) ) ) l2 (cdr l2) ) ) ( (last l2)) ) ) ) l1 ) ) Quote Link to comment Share on other sites More sharing options...
hanhphuc Posted October 12, 2014 Share Posted October 12, 2014 Thank you Lee, just because Doug Wilson's inspire so much so just sharing his great idea This was how he inspired me to figure out the step in many ways (defun Transpose (matx /) (apply 'mapcar (cons 'list matx))); VVA , Doug Wilson (Transpose '((1 2 3)(4 5 6))) ;((1 4) (2 5) (3 6)) (mapcar 'list '(1 2 3) '(4 5 6)) (apply 'mapcar '(list (1 2 3) (4 5 6))) (apply 'mapcar (cons 'list '((1 2 3)(4 5 6)))) (mapcar '(lambda(a b) (list a b)) '(1 2 3) '(4 5 6)) You do inspire me too Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 12, 2014 Share Posted October 12, 2014 Thank you Lee, just because Doug Wilson's inspire so much so just sharing his great idea This was how he inspired me to figure out the step in many ways (defun Transpose (matx /) (apply 'mapcar (cons 'list matx))); VVA , Doug Wilson (Transpose '((1 2 3)(4 5 6))) ;((1 4) (2 5) (3 6)) (mapcar 'list '(1 2 3) '(4 5 6)) (apply 'mapcar '(list (1 2 3) (4 5 6))) (apply 'mapcar (cons 'list '((1 2 3)(4 5 6)))) (mapcar '(lambda(a b) (list a b)) '(1 2 3) '(4 5 6)) You do inspire me too Thank you hanhphuc My point was more that the use of list/apply was unnecessary for this case, since the only purpose of the list function in this example was to enable the apply function to be used. Lee Quote Link to comment Share on other sites More sharing options...
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.