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

  • Similar Topics

    • By JulioHerrlein
      Dear All, 
       
      One interesting thing that could be implemented as a function could be a form of generating Negative Harmony.
      In the video below, there are some explanation of what it is and the origin in the Levy book.
      It was a trendy topic due to the Jacob Collier interview. And there are a lot of fun videos making versions of pop tunes using negative harmony.
       
      The way I understand it, it is simply a kind of mapping notes in relation to an axis, like in the figure below.
       

       
      So we need a function that could map a note in any register to another note in the closest register to the first on.
      So, any  C note will be mapped to G, all Db to F#, all D to F, all, Eb to E, all B to Ab, all Bb to A.
       
      It´s also possible to generate other mappings as well.
       
      I think that replace map or substitute map can do the job, but I´m not sure (I will try), but I find interesting to post it here to explore the idea.
       
      All the best,
       
      Julio
       
      It´s kind of funny to sse in this por versions how every is upside down and how you can generate an entirely new song from exactly the same material.
       
       
       
      POP TUNES with negative harmony:
       
       
       
       
       
    • By AM
      here is a sketch for an alternative "binary-(or element-)layer-FUNCTION
       
      (defun element-layer (lists &key (rnd nil)) (let ((lists (if (null rnd) lists (rnd-order lists :list t)))) (car (last (loop for x in (rest lists) with list = (car lists) collect (setf list (loop for i in list with cnt = 0 when (equal i 0) collect (nth cnt x) and do (incf cnt) else collect i))))))) (element-layer (list '(1 0 0 1 1 0 0 1 0 0 0 0) '(0 2 3 0 4 5 0 6 0 7 8 0) '(11 12 13 14 15 16 17)) :rnd nil) => (1 11 2 1 1 3 12 1 4 5 13 6) ;;; hierarchic: every 0's will be replaced by the values from the next/sub-list...  
    • By InLight-Tone
      Just bought into OM, excellent software love it.
       
      If you could be so kind, what is the key sequence to call up the search function? That last key symbol is a mystery to me.
       
      Thanks!
×
×
  • Create New...