Lisp:defmacro与& optional和& body

时间:2014-12-24 22:19:56

标签: lisp common-lisp sbcl

我写了一个快速而又脏的宏来计算lisp代码。但是,我现在面临的问题是我想在函数中包含一个可选的输出流。但是,我无法弄清楚如何同时使用&optional中的&bodydefmacro参数。我找了一些例子,但只发现defun的那些我认为我理解的。我无法弄清楚为什么这对我失败了。任何提示:

(defmacro timeit (&optional (out-stream *standard-output*) (runs 1) &body body)
  "Note that this function may barf if you are depending on a single evaluation
  and choose runs to be greater than one. But I guess that will be the
  caller's mistake instead."
  (let ((start-time (gensym))
        (stop-time (gensym))
        (temp (gensym))
        (retval (gensym)))
    `(let ((,start-time (get-internal-run-time))
           (,retval (let ((,temp))
                      (dotimes (i ,runs ,temp)
                        (setf ,temp ,@body))))
           (,stop-time (get-internal-run-time)))
       (format ,out-stream
               "~CTime spent in expression over ~:d iterations: ~f seconds.~C"
               #\linefeed ,runs
               (/ (- ,stop-time ,start-time)
                  internal-time-units-per-second)
               #\linefeed)
       ,retval)))

这就是我打算使用代码的方式:

(timeit (+ 1 1)) ; Vanilla call
(timeit *standard-output* (+ 1 1)) ; Log the output to stdout
(timeit *standard-output* 1000 (+ 1 1)) ; Time over a 1000 iterations.

我认为,defmacro(defmacro mac2 (&optional (a 2 b) (c 3 d) &rest x) `'(,a ,b ,c ,d ,x)) => MAC2 (mac2 6) => (6 T 3 NIL NIL) (mac2 6 3 8) => (6 T 3 T (8)) 的{​​{1}}是一个类似的想法。

(timeit (+ 1 1)) ; Vanilla call
(timeit :out-stream *standard-output* (+ 1 1)) ; Log the output to stdout
(timeit :out-stream *standard-output* :runs 1000 (+ 1 1)) ; Time over a 1000 iterations.

编辑:关键字参数

上面显示的用法显然有缺陷。也许,这更好:

{{1}}

感谢。

3 个答案:

答案 0 :(得分:3)

应该如何运作?

如何检测到第一件事是可选流?

(timeit a)      ; is a the optional stream or an expression to time?
(timeit a b)    ; is a the optional stream or an expression to time?
(timeit a b c)  ; is a the optional stream or an expression to time?

我会避免这样的宏观角色。

通常我更喜欢:

(with-timings ()
  a b c)

并使用流

(with-timings (*standard-output*)
  a b c)

第一个列表给出了可选参数。列表本身不是可选的。

该宏应该更容易编写。

通常可能没有必要指定流:

(let ((*standard-output* some-stream))
  (timeit a b c))

你可以实现你想要的,但我不会这样做:

(defmacro timeit (&rest args)
   (case (length args)
     (0 ...)
     (1 ...)
     (otherwise (destructuring-bind (stream &rest body) ...))))

答案 1 :(得分:1)

解决方案:使用非可选关键字arglist

(defmacro timeit ((&key
                    (to-stream *standard-output*)
                    (with-runs 1))
                  &body body)
  "Note that this function may barf if you are depending on a single evaluation
  and choose with-runs to be greater than one. But I guess that will be the
  caller's mistake instead."
  (let ((start-time (gensym))
        (stop-time (gensym))
        (temp (gensym))
        (retval (gensym))
        (elapsed-time (gensym)))
    `(let* ((,start-time (get-internal-run-time))
            (,retval (let ((,temp))
                       (dotimes (i ,with-runs ,temp)
                         (setf ,temp ,@body))))
            (,stop-time (get-internal-run-time))
            (,elapsed-time (/ (- ,stop-time ,start-time)
                              internal-time-units-per-second)))
       (format ,to-stream
               (concatenate 'string
                            "~CAverage (total) time spent in expression"
                            " over ~:d iterations: ~f (~f) seconds.~C")
               #\linefeed
               ,with-runs
               ,elapsed-time
               (/ ,elapsed-time ,with-runs)
               #\linefeed)
       ,retval)))

基于Rainer的评论。

使用模式:

(timeit nil (+ 1 1)) ; Vanilla case
(timeit (:to-stream *standard-output*) (+ 1 1)) ; Log to stdout
(timeit (:with-runs 1000) (+ 1 1)) ; Evaluate 1000 times
(timeit (:with-runs 1000 :to-stream *standard-output*) (+ 1 1)) ; Evaluate 1000 times and log to stdout

答案 2 :(得分:1)

我普遍认为这些论点通常应该在一个单独的列表中提供,该列表是宏的第一个参数。这在 with - 类型的宏中尤为常见。其他一些答案已经说明了如何做到这一点,但我认为它也是一个很好的宏写入技术,首先编写实现主要功能的功能版本,然后然后编写一个宏版本。虽然 这里的方法有可能为函数调用开销增加一些时间,但这个方法并不太难。

(defun %timeit (function &optional (runs 1) (stream *standard-output*))
  (let ((start (get-internal-run-time))
        ret
        stop)
    (prog1 (dotimes (i runs ret)
             (declare (ignorable i))
             (setf ret (funcall function)))
      (setf stop (get-internal-run-time))
      (format stream "~&Time spent in ~a iterations: ~f seconds."
              runs
              (/ (- stop start) internal-time-units-per-second)))))

(defmacro timeit ((&optional (runs 1) (stream *standard-output*)) &body body)
  `(%timeit #'(lambda () ,@body) ,runs ,stream))

CL-USER> (timeit (10000000) (1+ most-positive-fixnum))
Time spent in 10000000 iterations: 0.148 seconds.
4611686018427387904