Jonathan Handojo Posted March 29, 2020 Posted March 29, 2020 (edited) Hi guys, How can you reverse a list from x to y? For example, if the function is (defun reversenth (lst a b) ... ) where lst is the list, a and b are integers indicating the indices, then (reversenth '(0 1 2 3 4 5 6 7 8 9 10) 3 6) will return (0 1 2 6 5 4 3 7 8 9 10) Thanks, Jonathan Handojo Edited March 29, 2020 by Jonathan Handojo Quote
Lee Mac Posted March 29, 2020 Posted March 29, 2020 (edited) Here's a basic method: (defun reversesub ( l a b / i p r s ) (setq i 0) (foreach x l (cond ( (< i a) (setq p (cons x p))) ( (<= i b) (setq r (cons x r))) ( (setq s (cons x s))) ) (setq i (1+ i)) ) (append (reverse p) r (reverse s)) ) _$ (reversesub '(0 1 2 3 4 5 6 7 8 9 10) 3 6) (0 1 2 6 5 4 3 7 8 9 10) Edited March 29, 2020 by Lee Mac Quote
dlanorh Posted March 29, 2020 Posted March 29, 2020 21 minutes ago, Jonathan Handojo said: Hi guys, How can you reverse a list from x to y? For example, if the function is (defun reversenth (lst a b) ….. ) where lst is the list, a and b are integers indicating the indices, then (reversenth '(0 1 2 3 4 5 6 7 8 9 10) 3 6) will return (0 1 2 6 5 4 3 7 8 9 10) Thanks, Jonathan Handojo You can try or adapt this (defun rh:reva2b ( a b lst / rh:x2y p r s) (defun rh:x2y (a b lst / cnt n_lst) (setq cnt 1) (mapcar '(lambda (x) (if (< (1- a) cnt (1+ b)) (setq n_lst (cons x n_lst) cnt (1+ cnt)) (setq cnt (1+ cnt)))) lst) (reverse n_lst) ) (setq p (rh:x2y 1 a lst) r (rh:x2y a b lst) s (rh:x2y b 0 lst)) (apply 'append (list p (reverse r) s)) ) Quote
Lee Mac Posted March 29, 2020 Posted March 29, 2020 (edited) Another: (defun reversesub2 ( l a b / i ) (setq i -1) (mapcar '(lambda ( x ) (if (<= a (setq i (1+ i)) b) (nth (- b i (- a)) l) x)) l) ) Edited March 29, 2020 by Lee Mac 1 Quote
Jonathan Handojo Posted March 29, 2020 Author Posted March 29, 2020 Lol, same idea I had, I thought there was a cleaner approach. Thanks dlanorh, I'll use this Quote
Jonathan Handojo Posted March 29, 2020 Author Posted March 29, 2020 (edited) 7 minutes ago, Lee Mac said: Another: (defun reversesub2 ( l a b ) (setq i -1) (mapcar '(lambda ( x ) (if (<= a (setq i (1+ i)) b) (nth (- b i (- a)) l) x)) l) ) Okay, that certainly didn't cross my mind. A complex calculation at that. Thanks Lee. Edited March 29, 2020 by Jonathan Handojo Quote
dlanorh Posted March 29, 2020 Posted March 29, 2020 18 minutes ago, Jonathan Handojo said: Lol, same idea I had, I thought there was a cleaner approach. Thanks dlanorh, I'll use this Don't, it errors. It should be (defun rh:reva2b ( a b lst / rh:x2y p r s) (defun rh:x2y (a b lst / cnt n_lst) (setq cnt 1) (mapcar '(lambda (x) (if (< (1- a) cnt (1+ b)) (setq n_lst (cons x n_lst) cnt (1+ cnt)) (setq cnt (1+ cnt)))) lst) (reverse n_lst) ) (setq p (rh:x2y 1 a lst) r (rh:x2y (1+ a) (1+ b) lst) s (rh:x2y (+ b 2) (length lst) lst) ) (apply 'append (list p (reverse r) s)) ) Quote
Grrr Posted March 29, 2020 Posted March 29, 2020 (edited) (defun reversenth ( L a b ) ( (lambda (f L a b) (if (and L (apply '= (cons 'INT (mapcar 'type (list a b))))) (f L a (1- b) 0 nil))) (lambda ( L a b i c ) (cond ( (not L) c) ( (<= a i b) (f (cdr L) a b (1+ i) (append (list (car L)) c)) ) ( (append (list (car L)) c (f (cdr L) a b (1+ i) nil)) ) ) ) L a b ) ); defun Another: ; (reversenth2 '(0 1 2 3 4 5 6 7 8 9 10) 3 6) (defun reversenth2 ( L a b / i tmp ) (if (> b (length L)) (setq b (1- (length L))) ) (if (< a 0) (setq a 0) ) (append (progn (repeat (setq i a) (setq tmp (cons (nth (setq i (1- i)) L) tmp)) ) tmp ; (0 1 2) ) (progn (repeat (setq tmp nil i (1+ (- b a))) (setq tmp (cons (nth (+ a (setq i (1- i))) L) tmp)) ); repeat (reverse tmp) ; (6 5 4 3) ) (progn (repeat (setq tmp nil i (- (length L) (1+ b))) (setq tmp (cons (nth (+ 1 b (setq i (1- i))) L) tmp)) ) tmp ; (6 7 8 9) ) ); append ); defun Edited March 29, 2020 by Grrr Quote
hanhphuc Posted March 30, 2020 Posted March 30, 2020 (defun foo ( l i j / l1) (setq l1 (mapcar ''((a b) (member a b))(list (1- i) j (1+ j))(list (reverse l) (reverse (member i l)) l))) (apply 'append (cons (reverse (car l1)) (cdr l1))) ) (foo '( 0 1 2 3 4 5 6 7 8 9 10 ) 3 6 ) ;; sorted nth list ;(0 1 2 6 5 4 3 7 8 9 10) Quote
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.
Note: Your post will require moderator approval before it will be visible.