sharp-back syntax
- From: nallen05@xxxxxxxxx
- Date: 29 Mar 2007 17:46:30 -0700
I hate having to type l-a-m-b-d-a over and over again...
luckily it's easy to add syntax to Lisp:
(defun sharp-back-var-p (x)
(when (symbolp x)
(let ((n (symbol-name x)))
(when (> (length n) 0)
(char= (char n 0)
#\?)))))
(defun sharp-back-expand (form)
(let (vs)
(labels ((rfn (x)
(if (sharp-back-var-p x)
(pushnew x vs)
(when (listp x)
(mapc #'rfn x)))))
(rfn form)
`(lambda ,(nreverse vs) ,form))))
(set-dispatch-macro-character #\# #\`
#'(lambda (s c1 c2)
(declare (ignore c1 c2))
(sharp-back-expand (read s))))
usage:
#`(> 4 5) => (lambda () (> 4 5))
#`(= ?x 5) => (lambda (?x) (= ?x 5))
#`(print (+ ?a ?b)) => (lambda (?a ?b) (print (+ ?a ?b)))
#`(list ?x ?y ?x ?x ?y) => (lambda (?x ?y) (list ?x ?y ?x ?x ?y)
(mapcar #`(> ?x ?y) list1 list2) => (mapcar (lambda (?x ?y) (> ?x ?y))
list1 list2)
hth
Nick
.
- Follow-Ups:
- Re: sharp-back syntax
- From: js@xxxxxxxxxxxxxx
- Re: sharp-back syntax
- From: Rob Warnock
- Re: sharp-back syntax
- Prev by Date: Re: lexical scoping
- Next by Date: Re: How to change directory/path in SBCL?
- Previous by thread: Low-level, Efficient Ternary Search Trees for Storing Strings?
- Next by thread: Re: sharp-back syntax
- Index(es):