Write a function ORDERED which returns true if the numbers in a list
are in ascending or descending order.
For example:
(ordered '(3 5 7 10))
returns: T
(ordered '(8 6 5 3))
returns:T
but:
(ordered '(5 4 6 7))
returns: nil
;;; Returns a string and 'T' if the list is in a desc. or asc. order, and NIL otherwise
(defun ordered (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 list dlist)
(format nil "~%The numbers in ~a are in a descending order. ~%T" list))
((equal list alist)
(format nil "~%The numbers in ~a are in an ascending order. ~%T" list))))))))
(defun asc-or-desc (x)
(if (> (length x) 1)
(funcall
(lambda (y pred)
(every (lambda (z)
(when (funcall pred y z)
(setf y z))) (cdr x)))
(first x)
(if (> (first x) (second x)) '> '<)) t))
(asc-or-desc '(1 2 3 4)) ; T
(asc-or-desc '(4 3 2 1)) ; T
(asc-or-desc '(4 3 1 2)) ; NIL
Users browsing this forum: Google [Bot], porky11 and 3 guests