【问题标题】:Lisp: defmacro with &optional and &bodyLisp:带有可选和正文的 def 宏
【发布时间】:2014-12-24 22:19:55
【问题描述】:

我编写了一个快速而肮脏的宏来计时 lisp 代码。但是,我现在面临的问题是我想在函数中包含一个可选的输出流。但是,我不知道如何在defmacro 中同时使用&optional&body 参数。我查找了示例,但只找到了我认为我理解的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 上的hyperspec 中找到的这个是一个类似的想法。

(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)) 

编辑:关键字参数

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

(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.

谢谢。

【问题讨论】:

  • 但是你知道已经有一个宏可以做到这一点吗? TIME.
  • 你写了一个函数?我看到的只是一个...
  • 对不起,我是说宏。已更正。另外,我知道TIME 会这样做。这更适合练习编写宏。另外,我想包括一些时间没有的东西,比如迭代次数和日志记录等。
  • 这意味着你不能写...(timeit 1000 (+ 1 1))?
  • 是的,同意。我刚刚在你的回答下发表了评论。我认为可选的关键字参数可能更合适。我会努力实现的。

标签: lisp common-lisp sbcl


【解决方案1】:

这应该如何工作?

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

(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) ...))))

【讨论】:

  • 嗯...我明白你在说什么并且同意这会更容易写但是你的意思是这种&optional 后跟&body 成语太难了(不可能?)达到?
  • @asb:先不要写宏。举例说明代码在哪里使用了这样的宏,看看它是否有意义,然后尝试编写宏来实现语法。您需要FIRST 来获取语法,然后然后 编写宏。你现在有一个宏,我们必须猜测它可能会被如何使用......对我来说,完全不清楚这样的 arglist 是否有意义。
  • 添加了示例调用和 hyperspec 中的类似内容。
  • 好的,我明白为什么这没有意义了。也许关键字参数在这里会更合适。
  • 感谢您的帮助。
【解决方案2】:

解决方案:使用非可选关键字 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 的 cmets。

使用模式:

(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

【讨论】:

    【解决方案3】:

    我的普遍看法是,这些类型的参数通常应该在一个单独的列表中提供,该列表是宏的第一个参数。这在 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
    

    【讨论】:

      猜你喜欢
      • 2011-05-07
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      相关资源
      最近更新 更多