Improvement to Common Lisp:
Macro multiple-value-constantly
(multiple-value-constantly form*)
multiple-value-constantly evaluates each form, gathers all the values together, and yields a constant function that takes any number of arguments and returns the gathered values. It’s the spawn of multiple-value-call and constantly.
constantly could be defined as (defun constantly (it) (multiple-value-constantly it)).
Here’s my implementation:
(defmacro multiple-value-constantly (&rest forms)
`(multiple-value-call
(lambda (&rest l) (lambda (&rest any)
(declare (ignore any))
(values-list l)))
,@forms))
May induce funny feeling in Lisp programmers:
(setf (symbol-function 'constantly-nothing)
(multiple-value-constantly))
(constantly-nothing)
Of course, you have to be careful using multiple values; they can be tricky.
2007-04-26 at 13:34:38
I don’t get why it’s a macro. Why not a function?
(defun multiple-value-constantly (&rest values)
(lambda (&rest rest)
(declare (ignore rest))
(values-list values)))
2007-04-26 at 13:37:30
Ah, I see.
2007-04-26 at 14:15:05
Thanks for asking Zach. It looks like you’ve enlightened yourself, but for the benefit of readers following along at home:
It’s a macro not a function for the same reason that multiple-value-call is a special operator, so that it can accumulate the multiple values that each form produces:
* (funcall (multiple-value-constantly (floor 5 2)))
2
1