PatrickMimran Posted November 20, 2016 Share Posted November 20, 2016 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 Quote Link to comment Share on other sites More sharing options...
Stephane Boussuge Posted November 20, 2016 Share Posted November 20, 2016 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 )))) ........ Quote Link to comment Share on other sites More sharing options...
PatrickMimran Posted November 20, 2016 Author Share Posted November 20, 2016 Merci Super Patrick Quote Link to comment Share on other sites More sharing options...
PatrickMimran Posted November 20, 2016 Author Share Posted November 20, 2016 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) ))) Quote Link to comment Share on other sites More sharing options...
opmo Posted November 20, 2016 Share Posted November 20, 2016 Please check the code again it produces an error. Quote Link to comment Share on other sites More sharing options...
PatrickMimran Posted November 20, 2016 Author Share Posted November 20, 2016 Sorry i cannot find it Quote Link to comment Share on other sites More sharing options...
Stephane Boussuge Posted November 21, 2016 Share Posted November 21, 2016 Je regarde et vous répond dès que possible. S. Quote Link to comment Share on other sites More sharing options...
Stephane Boussuge Posted November 21, 2016 Share Posted November 21, 2016 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. Quote Link to comment Share on other sites More sharing options...
PatrickMimran Posted November 22, 2016 Author Share Posted November 22, 2016 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 Quote Link to comment Share on other sites More sharing options...
Stephane Boussuge Posted November 22, 2016 Share Posted November 22, 2016 (position-insert 2 '((g8 g3 g6)) '(c4 c4 c4 c4 c4)) => (c4 c4 (g8 g3 g6) c4 c4 c4) S. lviklund 1 Quote Link to comment Share on other sites More sharing options...
PatrickMimran Posted November 22, 2016 Author Share Posted November 22, 2016 Thank you 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.