SBCL defconstant workaround

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.
Post Reply
hayden
Posts: 7
Joined: Fri Jan 04, 2013 1:27 pm

SBCL defconstant workaround

Post by hayden » 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:

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

Paul
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

Re: SBCL defconstant workaround

Post by Paul » Sat Jan 19, 2013 6:59 pm

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

hayden
Posts: 7
Joined: Fri Jan 04, 2013 1:27 pm

Re: SBCL defconstant workaround

Post by hayden » Sat Jan 19, 2013 7:50 pm

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

Paul
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

Re: SBCL defconstant workaround

Post by Paul » Sun Jan 20, 2013 2:41 pm

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

hayden
Posts: 7
Joined: Fri Jan 04, 2013 1:27 pm

Re: SBCL defconstant workaround

Post by hayden » Mon Jan 21, 2013 7:09 am

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

Post Reply