SBCL defconstant workaround
Posted: Thu Jan 17, 2013 4:26 pm
This probably only affects a few people out of thousands, but I just thought I would upload it anyways.
I couldn't find a "Not Enough to Justify it's own Thread" thread either. Anyways, in ANSI Common Lisp, Graham, Paul writes some functions that can be used to parse dates, but if you're using SBCL, you end up finding out that some weird bug involving defconstant gives some errors. Having said that, I don't mean that it doesn't execute the code, because I found out that the function was usable, but I would much rather not have to fiddle with SBCL error explorer, so I worked around.
If you completely omit the usage of defconstant, you can parse dates using the following code:
Very little code has to be changed, so it's a trivial fix. As long as you are careful not to accidentally let your code setf months to something else.
PS, if you aren't following along with ANSI Common Lisp, the constituent function is very simple
It checks a character to make sure it is not equal to a space (ie " ") and that it is also a graphical character. A graphical character is something you can print out. It it returns true or nil.
Ok, that's all, I hope this helps somebody eventually.
I couldn't find a "Not Enough to Justify it's own Thread" thread either. Anyways, in ANSI Common Lisp, Graham, Paul writes some functions that can be used to parse dates, but if you're using SBCL, you end up finding out that some weird bug involving defconstant gives some errors. Having said that, I don't mean that it doesn't execute the code, because I found out that the function was usable, but I would much rather not have to fiddle with SBCL error explorer, so I worked around.
If you completely omit the usage of defconstant, you can parse dates using the following code:
Code: Select all
(defvar months (vector "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec"))
(defun parse-month (str)
(let ((p (position str months :test #'string-equal)))
(if p
(+ 1 p)
nil)))
;; checks a string to the list of month names and adds 1 to the result
;; so that it matches up with the expectation of the month. ie months are not
;; zero indexed
(defun parse-date (str)
(let ((toks (tokens str #'constituent 0)))
(list (parse-integer (first toks))
(parse-month (second toks))
(parse-integer (third toks)))))
PS, if you aren't following along with ANSI Common Lisp, the constituent function is very simple
Code: Select all
(defun constituent (c)
(and (graphic-char-p c)
(not (char= c #\ ))))
Ok, that's all, I hope this helps somebody eventually.