Common Lisp で 1 を返す関数 lambda constantly *
;; 次の3つは 0 を返す関数
(lambda () 1)
(constantly 1)
#'*
;; 昨日の↓は
(subseries (scan-fn t (lambda () 1) #'1+) 0 10)
;; こう書ける
(subseries (scan-fn t #'* #'1+) 0 10)
ただし (constantly 1)
以外は引数なしでコールしなければならない。 (constantly 1)
はどんな引数を渡しても1が返る。
SBCL での constantly の定義がこれ。
(defun constantly (value)
#!+sb-doc
"Return a function that always returns VALUE."
(lambda ()
;; KLUDGE: This declaration is a hack to make the closure ignore
;; all its arguments without consing a &REST list or anything.
;; Perhaps once DYNAMIC-EXTENT is implemented we won't need to
;; screw around with this kind of thing. -- WHN 2001-04-06
(declare (optimize (speed 3) (safety 0)))
value))
すごいよね?
constantly の存在を今日まで知らなくて g000001 に教えてもらった。
0 件のコメント:
コメントを投稿