Jump to content

Split one list to "one" list


vlisp

Recommended Posts

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 by vlisp
add code
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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 :lol:

 

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.

Link to comment
Share on other sites

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

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.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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