From: Pascal Bourguignon
Subject: Re: LISP PROGRAMMING
Date: 
Message-ID: <87ekjyn2e8.fsf@thalassa.informatimago.com>
·············@gmail.com (sharda mishra) writes:

> Hello every one
> 
> PLEASE help me  writing the following program in LISP language
> ;-
> 
> Write a LISP function ODD-REVERSE  which reverses the elements
> occuring at odd numbered position but keeps the elements at even
> numbered position in their given position.
> 
> for example:-
> 
> ODD-REVERSE  '(5 7 (6 4 )3 12 (2 1) 9)) returns the list
> 
> (9 7  12  3 (6 4) (2 1) 5)
> 
> thank a lot

There are so many ways to do it!  Why don't you exerce some creativity
and try one?

(Note that the example you show is actually even-reverse. Items are
numbered from 0. If you don't believe me, try (elt '(a b c) 0) or (nth
0 '(a b c))).


One of my favorites is:

(defun odd-reversed (list1 list2)
  (and
    (= (length list1) (length list2))
    (equal
     (nreverse
      (delete
       nil
       (let ((odd nil)) 
         (maplist (lambda (x) (unless (setf odd (not odd)) (car x))) list1))))
     (delete 
      nil
      (let ((odd nil)) 
        (maplist (lambda (x) (unless (setf odd (not odd)) (car x))) list2))))
    (equal
     (let ((odd nil)) 
       (maplist (lambda (x) (when (setf odd (not odd)) (car x))) list1))
     (let ((odd nil)) 
       (maplist (lambda (x) (when (setf odd (not odd)) (car x))) list2))
     )));;odd-reversed


(defun odd-reverse-d (list)
  (loop for reversed = (do ((items (copy-seq list))
                            (reversed '()))
                           ((null items) reversed)
                         (let ((i (random (length items))))
                           (push (elt items i) reversed)
                           (if (zerop i)
                               (setf items (cdr items))
                               (setf items (append (subseq items 0 i) 
                                                   (subseq items (1+ i)))))))
        until (odd-reversed list reversed)
        finally (return reversed)));;odd-reverse-d

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Voting Democrat or Republican is like choosing a cabin in the Titanic.