Jump to content

Have several result options in a function


Recommended Posts

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 

 

 

 

 

Link to comment
Share on other sites

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 ))))
   
       ........

 

Link to comment
Share on other sites

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)     )))

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy