tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
5
Task/S-Expressions/Common-Lisp/s-expressions-1.lisp
Normal file
5
Task/S-Expressions/Common-Lisp/s-expressions-1.lisp
Normal 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
|
||||
16
Task/S-Expressions/Common-Lisp/s-expressions-2.lisp
Normal file
16
Task/S-Expressions/Common-Lisp/s-expressions-2.lisp
Normal 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)))))
|
||||
24
Task/S-Expressions/Common-Lisp/s-expressions-3.lisp
Normal file
24
Task/S-Expressions/Common-Lisp/s-expressions-3.lisp
Normal 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)) "]")))
|
||||
7
Task/S-Expressions/Common-Lisp/s-expressions-4.lisp
Normal file
7
Task/S-Expressions/Common-Lisp/s-expressions-4.lisp
Normal 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))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue