Posted November 20, 20168 yr Hello i tried to have a function who could move one atom or a list anyplace in another list , but now i would like to have the options to move this atom or list with or without parenthesis , see option a option b option a2 option b2 . I have no idea how i can implement several result options in a function , could you please explain me how to do that Thank you Patrick her are the functions i'd use as helping functions (defun list-diff (L1 L2) (cond ((null L1) nil) ((null (member (first L1) L2)) (cons (first L1) (list-diff (rest L1) L2))) (t (list-diff (rest L1) L2)) ) ) (defun hasSublistp (lst) (cond ((null lst) nil) ((listp (first lst)) t) (t (hasSublistp (rest lst))))) This is the final one (defun consxp (rang item lis ) (setq oldlist lis) (setq newlist ( nthcdr rang oldlist )) (setq litem (list item)) (cond ( ( and ( listp item ) (hassublistp oldlist )) ; OPTION A (append (list-diff oldlist newlist ) (list item) (nthcdr rang oldlist ))) ; OPTION B (append (list-diff oldlist newlist ) item (nthcdr rang oldlist ))) (( and ( atom item ) (hassublistp oldlist )) ; OPTION A2 (append (list-diff oldlist newlist ) (cons (list item) (nthcdr rang oldlist )))) ; OPTION B2 (append (list-diff oldlist newlist ) (cons item (nthcdr rang oldlist )))) (( listp item ) (append (list-diff oldlist newlist ) item (nthcdr rang oldlist ))) (( atom item ) (append (list-diff oldlist newlist ) (cons item (nthcdr rang oldlist )))))) ; ( consxp 2 '( a d a) '(a (d) c )) option a ) for example
November 20, 20168 yr Something like that using conditionnal "if" : (defun consxp (rang item lis &key (option1 a) (option2 a)) ...... (cond ( ( and ( listp item ) (hassublistp oldlist )) (if (equal option1 a) (append (list-diff oldlist newlist ) (list item) (nthcdr rang oldlist )) (append (list-diff oldlist newlist ) item (nthcdr rang oldlist )))) (( and ( atom item ) (hassublistp oldlist )) (if (equal option2 a) (append (list-diff oldlist newlist ) (cons (list item) (nthcdr rang oldlist )))) (append (list-diff oldlist newlist ) (cons item (nthcdr rang oldlist )))) ........
November 20, 20168 yr Author Désolé mais je n'arrive pas à le faire marcher en suivant vos instructions (defun consxp (rang item lis &key (option1 a) (option2 a) ) (setq oldlist lis) (setq newlist ( nthcdr rang oldlist )) (setq litem (list item)) (cond ( ( and ( listp item ) (hassublistp oldlist )) (if (equal option1 a)) (append (list-diff oldlist newlist ) (list item) (nthcdr rang oldlist )) (append (list-diff oldlist newlist ) item (nthcdr rang oldlist ))) (( and ( atom item ) (hassublistp oldlist )) (if (equal option2 a) (append (list-diff oldlist newlist ) (cons (list item) (nthcdr rang oldlist )))) (append (list-diff oldlist newlist ) (cons item (nthcdr rang oldlist )))) (( listp item ) (append (list-diff oldlist newlist ) item (nthcdr rang oldlist ))) (( atom item ) (append (list-diff oldlist newlist ) (cons item (nthcdr rang oldlist )))))) ( consxp 2 '(a d a) '(a (d) c) )))
November 21, 20168 yr i think POSITION-INSERT can do what you want. Example: (position-insert '((2 3 4)) '((a d a)) '((a (d) c))) => ((a (d) a d a c)) (position-insert '((2 3 4)) '(((a d a))) '((a (d) c))) => ((a (d) (a d a) (a d a) (a d a) c)) (position-insert '(2 3 4) '(((a d a))) '((a (d) c))) => ((a (d) (a d a) c)) Please, have a look to the documentation of POSITION-INSERT and to my example and let me know if it's works for your need. SB.
November 22, 20168 yr Author Yes it does the work very well but no for lists when i do: (position-insert 2 '(g8 g3 g6 ) '(c4 c4 c4 c4 c4) :type list) or (position-insert 2 '(g8 g3 g6 ) '(c4 c4 c4 c4 c4) :type 'list) Thanks Patrick
November 22, 20168 yr (position-insert 2 '((g8 g3 g6)) '(c4 c4 c4 c4 c4)) => (c4 c4 (g8 g3 g6) c4 c4 c4) S.
Create an account or sign in to comment