vlisp Posted January 17, 2017 Posted January 17, 2017 (edited) Hello to everyone, I want to split one list from any member from list and receive one list with begninig that element. Example: given list (1 5 3 6 8 2 10) Selected member : 3 The result : list (3 6 8 2 10 1 5) (setq l (list 1 5 3 6 8 2 10)) (setq x (member 3 l)) (setq res (append ?? x)) Thank you Edited January 17, 2017 by vlisp add code Quote
Grrr Posted January 17, 2017 Posted January 17, 2017 This may work: (defun foo ( n Lst ) (append (vl-member-if '(lambda (x) (= n x)) Lst) (reverse (cdr (vl-member-if '(lambda (x) (= n x)) (reverse Lst)))) ) ) _$ (foo 3 '(1 5 3 6 8 2 10)) (3 6 8 2 10 1 5) _$ Quote
vlisp Posted January 17, 2017 Author Posted January 17, 2017 Thank you Grrr! It is works perfect This may work: (defun foo ( n Lst ) (append (vl-member-if '(lambda (x) (= n x)) Lst) (reverse (cdr (vl-member-if '(lambda (x) (= n x)) (reverse Lst)))) ) ) _$ (foo 3 '(1 5 3 6 8 2 10)) (3 6 8 2 10 1 5) _$ Quote
David Bethel Posted January 17, 2017 Posted January 17, 2017 What should happen if the there are more than 1 of the key atom (3) in the list ? -David Quote
Lee Mac Posted January 17, 2017 Posted January 17, 2017 Another: (defun foo ( n l ) (repeat (cond ((vl-position n l)) (0)) (setq l (append (cdr l) (list (car l))))) ) Or: (defun bar ( n l / r ) (append (vl-member-if '(lambda ( x ) (cond ((= n x)) ((setq r (cons x r)) nil))) l) (reverse r) ) ) Or: (defun baz ( n l ) ( (lambda ( foo ) (foo n l nil)) (lambda ( n l a ) (cond ( (null l) (reverse a)) ( (= n (car l)) (append l (reverse a))) ( (foo n (cdr l) (cons (car l) a))) ) ) ) ) Quote
Grrr Posted January 17, 2017 Posted January 17, 2017 Thank you Grrr!It is works perfect No problem, but my (lazy and fast) suggestion doesn't account duplicate items, infact it has bug: _$ (foo 3 '(3 1 5 3 6 8 2 10)) (3 1 5 3 6 8 2 10 3 1 5) Just like David mentioned: What should happen if the there are more than 1 of the key atom (3) in the list ? -David @Lee, I have some trouble understanding your codes (especially the last one - 1.Is that called recursive lambda? 2.How this exactly works?) Quote
Lee Mac Posted January 17, 2017 Posted January 17, 2017 @Lee,I have some trouble understanding your codes (especially the last one - 1.Is that called recursive lambda? 2.How this exactly works?) The last example involves an anonymous lambda function being supplied as an argument to another anonymous lambda function, such that the symbol which is evaluated recursively within the lambda argument becomes a defined function when the outermost lambda function is evaluated - obfuscation at its finest The function could of course also be written more explicitly as: (defun foo ( itm lst ) (bar itm lst nil) ) (defun bar ( itm lst acc ) (cond ( (null lst) (reverse acc)) ( (= itm (car lst)) (append lst (reverse acc))) ( (bar itm (cdr lst) (cons (car lst) acc))) ) ) Here, the argument 'acc' is the accumulator which is populated with each recursive call - this is known as tail recursion. Quote
Grrr Posted January 17, 2017 Posted January 17, 2017 Lee, thanks for explaining! ATM I think that one of the biggest frustrations in AutoLISP are the recursions. Hopefully one day I'll level up at understanding them. Quote
Roy_043 Posted January 17, 2017 Posted January 17, 2017 Another: ; (CycleList 3 '(7 9 3 1 5 3 6 8 3 2 10)) => (3 1 5 3 6 8 3 2 10 7 9) (defun CycleList (n lst / len pre) (setq len (length lst)) (append (setq pre (member n lst)) (progn (setq pre (length pre)) (setq lst (reverse lst)) (while (/= len (+ pre (length (setq lst (cdr (member n lst))))))) (reverse lst) ) ) ) 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.