jradix

Three List Manipulation Exercises

Oct 12th, 2016
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defparameter *the-list* '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20))
  2.  
  3. (defun only-even (lst) (remove-if #'oddp lst))
  4.  
  5. (defun remove-every-nth (n lst)
  6.   (let ((i 0))
  7.     (remove-if
  8.      #'(lambda (x) (if (= (setf i (mod (incf i) n)) 0) t nil))
  9.      lst)))
  10.  
  11. (defun reverse-halves (lst)
  12.   (cond
  13.     ((null lst) nil)
  14.     ((= (length lst) 1) (copy-list lst))
  15.     (t (let* ((h (truncate (/ (length lst) 2)))
  16.           (l (copy-list lst))
  17.           (r (nthcdr h l)))
  18.      (setf (cdr (nthcdr (- h 1) l)) nil)
  19.      (setf (cdr (nthcdr (- (length r) 1) r)) l)
  20.      r))))
RAW Paste Data