Common Lisp Rankable / Sortable Enumerations
I wonder if there is any trick or library to get ratings in Common Lisp.
In general I define a set of enums like this
(deftype weekdays() '(member :sunday :monday :tuesday :wednesday :thursday :friday :saturday))
Of course, I can compare two variables containing the same day of the week, for example. if both contain: Wednesday.
But I want to compare two different weekdays like "Is: thursday later than: monday"? This brings me back to constants
(defconstant +sunday+ 0)
(defconstant +monday+ 1)
and so on...
but that seems to be bad style.
What's the best practice for this?
+3
source to share
1 answer
If you define items in a separate list, you can simply compare them by position:
(defparameter *days*
'(:sunday :monday :tuesday :wednesday :thursday :friday :saturday)
"Keywords indicating days of the week.")
(deftype day ()
"Type representing days. A day is an element of the list *DAYS*."
`(member ,@*days*))
(defun day< (day1 day2)
"Returns true if DAY1 is earlier in the week than DAY2, according to
the order specified in *DAYS*."
(< (position day1 *days*)
(position day2 *days*)))
(typep :monday 'day) ;=> T
(typep :fooday 'day) ;=> NIL
(day< :monday :friday) ;=> T
(day< :thursday :tuesday) ;=> NIL
+3
source to share