March 20Mar 20 Hello, OpusmodersI did (with a little help of Notebooklm) this cool function whose function is to concatenate 2 diferent lists with sublists, merging the first sublist of the list A with the first sublist of list B, with the options 1) to choose how many elements of each list to use; and2) the possibility of not only mix different numbers of elements in each list but also the possibility to select the starting element of the list to pick elements.3) The function also consider each list as a rotating element: for example, if 5 elements are required from the list (1 2 3), starting in the first element, the result will be (1 2 3 1 2), if the start is on the second element, the result will be (2 3 1 2 3) and so on.This is useful for me. Hope you like it.Best,Julio(defun get-rotated-elements (lst start count)"Extracts COUNT elements from LST starting at index START, wrapping around if needed."(let ((len (length lst)))(loop for i from 0 below countcollect (nth (mod (+ start i) len) lst))))(defun merge-rotated-sublists (list-a list-b counts-a counts-b starts-a starts-b)"Merges sublists from A and B based on specific counts and starting positions with rotation."(mapcar #'(lambda (sub-a sub-b n m s-a s-b)(append (get-rotated-elements sub-a s-a n)(get-rotated-elements sub-b s-b m)))list-a list-b counts-a counts-b starts-a starts-b))Example of Usage(setf list-a '((1 2 3 4) (a b c) (q w e r t)))(setf list-b '((4 5 6 7) (d e f) (a s d f g)))(merge-rotated-sublists list-a list-b '(2 1 5) '(1 5 3) '(0 2 2) '(0 0 1));; Result: ((1 2 4) (c d e f d e) (e r t q w s d f))(merge-rotated-sublists list-a list-b '(2 7 1) '(4 3 7) '(0 0 0) '(1 1 0));((1 2 5 6 7 4) (a b c a b c a e f d) (q a s d f g a s))
Create an account or sign in to comment