Page 1 of 1

any function is there

Posted: Fri Jul 01, 2011 1:21 pm
by murali
is there any funtion to test wherther the given list is in ascending or descendin order
I saw one funtion INSEQA but ts not working

Re: any function is there

Posted: Fri Jul 01, 2011 2:01 pm
by Indecipherable
Perhaps something like this? Lol, it is late by me so my code may seem a little messy today and everything :D
Murali, I really get the feeling that you are needing this code and the previous for some assignment of some sort. You do not even post any code as a guide-line, which raises suspicion. :?:

Here you go though:

Code: Select all

;;; By Indecipherable
;;; Checks if a list is in a disc. or asc. order, or neither.

(defun check (list)
(let ((list-dec (copy-list list)))
(let ((list-inc (copy-list list)))
(let ((alist (sort list-inc #'<)))
(let ((dlist (sort list-dec #'>)))

(cond
 ((equal alist list)
  (format t "The list is in an ascending order."))
 ((equal dlist list)
  (format t "The list is in a descending order.")))
  
(if
 (not (equal alist list))
 (if
  (not (equal dlist list))
  (format t "NO MATCH"))))))))

Re: any function is there

Posted: Sat Jul 02, 2011 2:40 am
by Konfusius

Code: Select all

(apply #'< list)

Re: any function is there

Posted: Sat Jul 02, 2011 6:49 am
by Indecipherable
Konfusius wrote:

Code: Select all

(apply #'< list)
Okay so obviously there are quite alot of ways :mrgreen:

Re: any function is there

Posted: Sat Jul 02, 2011 7:11 am
by Paul
Konfusius wrote:

Code: Select all

(apply #'< list)
That doesn't work, because it's supposed to check if the list is either ascending or descending. You could do

Code: Select all

(or (apply #'<= list) (apply #'>= list))
(you want <=/>= in case there are duplicates), but using APPLY to do this sort of thing is bad style: an arbitrary list of numbers isn't the packaged argument list of a function (think: call-arguments-limit), and there's no need to walk the list twice.

Code: Select all

(defun orderedp (list)
  (let ((order nil) (last (first list)))
    (dolist (x (rest list) (or order :EQUAL))
      (cond ((< x last)
	     (if (and order (eq order :ASCENDING))
		 (return nil)
		 (setq order :DESCENDING)))
	    ((> x last)
	     (if (and order (eq order :DESCENDING))
		 (return nil)
		 (setq order :ASCENDING))))
      (setq last x))))