Jump to content

Reverse list AFTER x Depending vl-position


hosneyalaa

Recommended Posts

Sorry English is not good

Hello all 
If possible, help If he has the following list 

(setq lst-pt '((2.91097 7.9291) (15.2064 6.99916) (12.7764 1.72037) (0.268183 0.311625)))

 

Depending on the location of the item

(reverseLITP lst-pt   2)

((2.91097 7.9291) (15.2064 6.99916) (0.268183 0.311625) (12.7764 1.72037))

 

OR 

(reverseLITP lst-pt   1)

((2.91097 7.9291) (0.268183 0.311625) (12.7764 1.72037) (15.2064 6.99916))

 

 

Thanks, thanks ALL

 

Link to comment
Share on other sites

Here is one way.

(defun reverse:list (lst pos / lft run new lft out)
  (and (< pos (length lst))
       (setq lft lst
             run -1
       )
       (repeat pos
         (setq new (cons (nth (setq run (1+ run)) lst) new)
               lft (cdr lft)
         )
       )
       (setq out (append (reverse new) (reverse lft)))
  )
  out
)
  • Thanks 1
Link to comment
Share on other sites

45 minutes ago, hosneyalaa said:

Sorry English is not good

Hello all 
If possible, help If he has the following list 

(setq lst-pt '((2.91097 7.9291) (15.2064 6.99916) (12.7764 1.72037) (0.268183 0.311625)))

 

Depending on the location of the item

(reverseLITP lst-pt   2)

((2.91097 7.9291) (15.2064 6.99916) (0.268183 0.311625) (12.7764 1.72037))

 

OR 

(reverseLITP lst-pt   1)

((2.91097 7.9291) (0.268183 0.311625) (12.7764 1.72037) (15.2064 6.99916))

 

 

Thanks, thanks ALL

 

 

Try this

 

(defun revlstp (lst p / nlst)
  (repeat p (setq nlst (cons (car lst) nlst) lst (cdr lst)))
  (reverse (append lst nlst))
)

 

  • Thanks 1
Link to comment
Share on other sites

Nice Tharwat & dlnorh

(defun foo (i l / n k)
  (setq n (length l))
  (cond
    ((not i)(reverse l))
    ((< i n)
      (repeat (1+ i)
        (setq k (cons (car l) k)
              l (cdr l)
        )
      )
       (apply 'append (mapcar 'reverse (list k l)))
    )
    (t nil)
  )
)

 

this reverse (1+ nth)

(setq lst '(1 2 3 4 5 6 7))
(foo nil lst) ;(7 6 5 4 3 2 1)
(foo 0 lst) ;(1 7 6 5 4 3 2)
(foo 3 lst) ;(1 2 3 4 7 6 5)
(foo 6 lst) ;(1 2 3 4 5 6 7)
(foo 7 lst) ; nil

 

 

(defun revnth (n lst)
;hp 25.06.2020
  ((lambda (f n l) (reverse (append (f (- (length lst) n) (reverse lst)) (f n lst))))
    (list '(n l) '(if (> n 0) (cons (nth (1- n) l) (f (1- n) l)))) n l )
  )

 

reverse at nth
(setq lst '( 1 2 3 4 5 6 7 8 9 10 ) )
(revnth 0 lst) ; (10 9 8 7 6 5 4 3 2 1 0 )
(revnth 2 lst) ; (0 1 10 9 8 7 6 5 4 3 2 )
 

 

 

 

Edited by hanhphuc
add revnth
  • Thanks 1
Link to comment
Share on other sites

Another

(defun _relist  (lst n)
      ((lambda (i / l m)
             (while (and (< i n) (setq v (Car lst)))
                   (setq l   (cons v l)
                         i   (1+ i)
                         lst (cdr lst))
                   )
             (append (reverse l) (reverse lst))
             )
            0
            )
      )

 

hanhphuc 

You just have to go the other way  than the rest of us don't you..  😀  < n before l st  >

(defun revnth (n lst)...
(defun foo (i l / n k)...

 

 

  • Thanks 1
Link to comment
Share on other sites

4 hours ago, pBe said:

 

hanhphuc 

You just have to go the other way  than the rest of us don't you..  😀  < n before l st  >

 

 

i didn't notice it, it's a habit putting list after n :)

perhaps subconsciousness like

cons

apply

mapcar 

vl-remove 

etc..

:beer:

 

 

 

  • Thanks 1
Link to comment
Share on other sites

 

On 6/25/2020 at 11:18 PM, pBe said:

Another


(defun _relist  (lst n)
      ((lambda (i / l m)
             (while (and (< i n) (setq v (Car lst)))
                   (setq l   (cons v l)
                         i   (1+ i)
                         lst (cdr lst))
                   )
             (append (reverse l) (reverse lst))
             )
            0
            )
      )

 

hanhphuc 

You just have to go the other way  than the rest of us don't you..  😀  < n before l st  >


(defun revnth (n lst)...
(defun foo (i l / n k)...

 

 

 

Thanks @pBe

 

 

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