Paste number 328394: Three List Manipulation Exercises

Paste number 328394: Three List Manipulation Exercises
Pasted by: jrx
When:4 years, 3 months ago
Share:Tweet this! | http://paste.lisp.org/+71E2
Channel:None
Paste contents:
Raw Source | XML | Display As
;; See : https://programmingpraxis.com/2016/10/11/three-list-manipulation-exercises/

(defparameter *the-list* '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20))

(defun only-even (lst) (remove-if #'oddp lst))

(defun remove-every-nth (n lst)
  (let ((i 0))
    (remove-if
     #'(lambda (x) (if (= (setf i (mod (incf i) n)) 0) t nil))
     lst)))

(defun reverse-halves (lst)
  (cond
    ((null lst) nil)
    ((= (length lst) 1) (copy-list lst))
    (t (let* ((h (truncate (/ (length lst) 2)))
	      (l (copy-list lst))
	      (r (nthcdr h l)))
	 (setf (cdr (nthcdr (- h 1) l)) nil)
	 (setf (cdr (nthcdr (- (length r) 1) r)) l)
	 r))))

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.