Skip to content

Instantly share code, notes, and snippets.

@lispm
Created July 8, 2016 17:22
Show Gist options
  • Select an option

  • Save lispm/7c009676fe6b86cff851bc8a1ba82d28 to your computer and use it in GitHub Desktop.

Select an option

Save lispm/7c009676fe6b86cff851bc8a1ba82d28 to your computer and use it in GitHub Desktop.
(defmacro comp ((e &rest qs) l2)
(if (null qs)
`(cons ,e ,l2)
(let ((q1 (car qs))
(q (cdr qs)))
(if (not (eq (cadr q1) '<-))
`(if ,q1
(comp (,e . ,q) ,l2) ,l2)
(let ((v (car q1))
(l1 (third q1))
(h (gensym "H-"))
(us (gensym "US-"))
(us1 (gensym "US1-")))
`(labels ((,h (,us)
(if (null ,us)
,l2
(let ((,v (car ,us))
(,us1 (cdr ,us)))
(comp (,e . ,q) (,h ,us1))))))
(,h ,l1)))))))
(defun open-bracket (stream ch)
(do ((l nil)
(c (read stream t nil t) (read stream t nil t)))
((eq c '|]|)
`(comp ,(reverse l) ()))
(push c l)))
(defun closing-bracket (stream ch)
'|]|)
(set-macro-character #\[ #'open-bracket)
(set-macro-character #\] #'closing-bracket)
(defun iota* (n &optional (start 0))
"Creates a list of n elements starting with element START.
START can be a number, a character, a string or a symbol."
(etypecase start
(number (loop for i from start
repeat n
collect i))
(character (loop with c = (char-code (or (and (characterp start) start) #\a))
for i from 0
repeat n
collect (code-char (+ c i))))
(string (loop with c = (char-code (or (and (stringp start) (aref start 0)) #\a))
for i from 0
repeat n
collect (string (code-char (+ c i)))))
(symbol (loop with c = (char-code (or (and (symbolp start) (aref (string start) 0)) #\a))
for i from 0
repeat n
collect (intern (string (code-char (+ c i))) (symbol-package start))))))
[(expt x 2) (x <- (iota* 11))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment