A function that just returns its argument?

In Common Lisp, is there a function in the standard library that simply returns the given parameter (i.e. does not manipulate data)? This function will be equivalent to (lambda (x) x) . I want to use it as the default value for an optional parameter. For example, such a function would replace (lambda (x) x) with:

 (defun some-function (value &optional (transformation (lambda (x) x))) (other-function (funcall transformation value)) 
+7
common-lisp
source share
3 answers

Take a look at identity :

IDENTIFICATION function

Syntax:

identifier object & Rightarrow; an object

Arguments and Values:

object - object .

Description:

Returns the object of the argument.

BTW, the ANSI CL standard defines nearly a thousand characters. You cannot learn it all night. In addition, Lisp is a language with a rich history, so if you want something "general purpose", it is likely that either the language provides or some kind of (semi) standard library.

Ask! Do not reinvent the wheel.

+10
source share

sds answer describes an identification function that matches the specification you requested: it's pretty simple (lambda (x) x) . However, it is worth noting that in a function such as

 (defun some-function (value &optional (transformation (lambda (x) x))) (other-function (funcall transformation value)) 

it may be more idiomatic to describe your transformation as a key and let nil indicate that the key function should not be applied to the value. This behavior is present in many common Lisp functions. For example, member accepts a key argument that is applied to each element of the sequence to create a value that is compared to the search element:

 CL-USER> (member nil '(1 2 3 4 nil 5 6 7 8)) (NIL 5 6 7 8) CL-USER> (member nil '(1 2 3 4 nil 5 6 7 8) :key 'oddp) (2 3 4 NIL 5 6 7 8) 

The default behavior is the same as if you passed the identifier as a key:

 CL-USER> (member nil '(1 2 3 4 nil 5 6 7 8)) (NIL 5 6 7 8) CL-USER> (member nil '(1 2 3 4 nil 5 6 7 8) :key 'identity) (NIL 5 6 7 8) 

However, the default value is not identity , but nil . HyperSpec for member says the key:

key is the designation for a function of one argument or nil .

This is stated in 17.2.1 Satisfying a two-argument test , which states that:

If the key argument is provided ::, it is the function designation, one argument that should be called with each Ei as an argument, and a Zi object that will be used for comparison. (If not: key argument, Zi Ei.)

If you want to accept this type of convention, then your code will be something like

 (defun some-function (value &optional transformation) (other-function (if (null transformation) value (funcall transformation value)))) 

In your case, this may not be a big difference, but it will avoid calling an additional function. In general, this can be useful because it makes it easier to pass key arguments to library functions without worrying about whether they are nil or not.

+10
source share

To expand a bit in the question, #'identity will return its argument if you have one argument. #'list will return several arguments to the list, and #'values will return them as multiple values, they will serve the same purpose, but they really have a completely different API.

+1
source share

All Articles