Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • This just came to me; I stole the idea from the implicitly-declared parameters in Perl 6.

    (defmacro fn (&rest body)
      (let* ((env (make-hash-table :test 'eq))
             (new-body (fn-expand env `(progn ,@body)))
             (parameters '()))
        (maphash #'(lambda (key &optional value)
                     (setq parameters (cons key parameters)))
        `(lambda ,(sort parameters #'string-lessp)
           ,@(cdr new-body))))

    (defun fn-expand (env body)
      (cond ((symbolp body)
             (when (let ((case-fold-search t))
                     (string-match "^\\^[a-z][-_a-z0-9]*$" (symbol-name body)))
               (puthash body t env))
            ((and (consp body) (symbolp (car body)))
             (when (eq (car body) 'fn)
               (throw "(fn BODY...) does not yet support nesting"))
             (cons (car body)
                   (mapcar #'(lambda (arg) (fn-expand env arg))
                           (cdr body))))

    Now you can do things like this:

    (fn (print "I like pie and %s" ^x) (* 3.14 ^x))

    (mapcar (fn (+ 7 ^x)) list)

    (maphash (fn (puthash ^key ^val new-hash)) old-hash)

    That last one in particular is something that can't be done with your f_x macro.

    Share and enjoy!