tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,5 @@
(defun lsquare-reader (stream char)
(declare (ignore char))
(read-delimited-list #\] stream t))
(set-macro-character #\[ #'lsquare-reader) ;;Call the lsquare-reader function when a '[' token is parsed
(set-macro-character #\] (get-macro-character #\) nil)) ;;Do the same thing as ')' when a ']' token is parsed

View file

@ -0,0 +1,16 @@
;;A list of unit tests. Each test is a cons in which the car (left side) contains the
;;test string and the cdr (right side) the expected result of reading the S-Exp.
(setf unit-tests
(list
(cons "[]" NIL)
(cons "[a b c]" '(a b c))
(cons "[\"abc\" \"def\"]" '("abc" "def"))
(cons "[1 2 [3 4 [5]]]" '(1 2 (3 4 (5))))
(cons "[\"(\" 1 2 \")\"]" '("(" 1 2 ")"))
(cons "[4/8 3/6 2/4]" '(1/2 1/2 1/2))
(cons "[reduce #'+ '[1 2 3]]" '(reduce #'+ '(1 2 3)))))
(defun run-tests ()
(dolist (test unit-tests)
(format t "String: ~23s Expected: ~23s Actual: ~s~%"
(car test) (cdr test) (read-from-string (car test)))))

View file

@ -0,0 +1,24 @@
(defun write-sexp (sexp)
"Writes a Lisp s-expression in square bracket notation."
(labels ((parse (sexp)
(cond ((null sexp) "")
((atom sexp) (format nil "~s " sexp))
((listp sexp)
(concatenate
'string
(if (listp (car sexp))
(concatenate 'string "["
(fix-spacing (parse (car sexp)))
"] ")
(parse (car sexp)))
(parse (cdr sexp))))))
(fix-spacing (str)
(let ((empty-string ""))
(unless (null str)
(if (equal str empty-string)
empty-string
(let ((last-char (1- (length str))))
(if (eq #\Space (char str last-char))
(subseq str 0 last-char)
str)))))))
(concatenate 'string "[" (fix-spacing (parse sexp)) "]")))

View file

@ -0,0 +1,7 @@
(setf unit-tests '(((1 2) (3 4)) (1 2 3 4) ("ab(cd" "mn)op")
(1 (2 (3 (4)))) ((1) (2) (3)) ()))
(defun run-tests ()
(dolist (test unit-tests)
(format t "Before: ~18s After: ~s~%"
test (write-sexp test))))