Page 1 of 1

SBCL defconstant workaround

Posted: Thu Jan 17, 2013 4:26 pm
by hayden
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:

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)))))

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

Code: Select all

(defun constituent (c)
  (and (graphic-char-p c)
	   (not (char= c #\  ))))
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.

Re: SBCL defconstant workaround

Posted: Sat Jan 19, 2013 6:59 pm
by Paul
hayden wrote: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.
It's not a bug (in SBCL). The value in a DEFCONSTANT form is restricted to being something that's EQL to itself each time it's evaluated (that's horrible phrasing, I know), so using a vector is illegal (that is, you can use a vector, but you have to make sure you return the same vector each time -- so you can't use VECTOR or #(...) for the value).

Re: SBCL defconstant workaround

Posted: Sat Jan 19, 2013 7:50 pm
by hayden
You aren't Paul Graham, are you?

Re: SBCL defconstant workaround

Posted: Sun Jan 20, 2013 2:41 pm
by Paul
hayden wrote:You aren't Paul Graham, are you?
No

Re: SBCL defconstant workaround

Posted: Mon Jan 21, 2013 7:09 am
by hayden
Paul wrote:
hayden wrote:You aren't Paul Graham, are you?
No
Aw. Well anyways, thanks for your insight, I appreciate the help