any function is there

Discussion of Common Lisp

any function is there

Postby murali » Fri Jul 01, 2011 1:21 pm

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
murali
 
Posts: 15
Joined: Fri Jul 01, 2011 10:11 am

Re: any function is there

Postby Indecipherable » Fri Jul 01, 2011 2:01 pm

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"))))))))
Don't take the FUN out of DEFUN !
User avatar
Indecipherable
 
Posts: 47
Joined: Fri Jun 03, 2011 5:30 am
Location: Behind you.

Re: any function is there

Postby Konfusius » Sat Jul 02, 2011 2:40 am

Code: Select all
(apply #'< list)
Konfusius
 
Posts: 62
Joined: Fri Jun 10, 2011 6:38 am

Re: any function is there

Postby Indecipherable » Sat Jul 02, 2011 6:49 am

Konfusius wrote:
Code: Select all
(apply #'< list)

Okay so obviously there are quite alot of ways :mrgreen:
Don't take the FUN out of DEFUN !
User avatar
Indecipherable
 
Posts: 47
Joined: Fri Jun 03, 2011 5:30 am
Location: Behind you.

Re: any function is there

Postby Paul » Sat Jul 02, 2011 7:11 am

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))))
Paul
 
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 2 guests

cron