This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

SBCL defconstant workaround

5 posts · 10074 views

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:
(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
(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.
/prog/'s fibonacci sort

Re: SBCL defconstant workaround

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

You aren't Paul Graham, are you?
/prog/'s fibonacci sort

Re: SBCL defconstant workaround

hayden wrote:You aren't Paul Graham, are you?
No

Re: SBCL defconstant workaround

Paul wrote:
hayden wrote:You aren't Paul Graham, are you?
No
Aw. Well anyways, thanks for your insight, I appreciate the help
/prog/'s fibonacci sort