Convert a FUNCTION to a STRING or SYMBOL in Common Lisp
Is it possible in common lisp to convert a function to a symbol or a string for further use? What i mean is to get a "+"
or #:|+|
from #'+
.
function common-lisp symbols
add a comment |
Is it possible in common lisp to convert a function to a symbol or a string for further use? What i mean is to get a "+"
or #:|+|
from #'+
.
function common-lisp symbols
I'm wondering where this need comes from.
– zut
Nov 15 '18 at 20:13
1
@zut In some other languages like Python, function objects have a name attribute, which could be useful some times (example would be implementing multimethods in Python using decorators). Maybe the OP is looking into something similar. Although OP could just wrap it in a cons cell of [function, name], it would be easier to know if CL has such a name attribute already, instead of having to wrap the function object in such an interface. Just guessing though.
– Byte
Nov 17 '18 at 2:51
1
@Byte In CL that would be the other way around. The function is a property of the symbol (the name) rather than the name being a property of the function. If you need both, you should use the symbol and access the function through it.
– jkiiski
Nov 17 '18 at 3:25
@jkiiski Agreed, but I don't think that holds true for function objects defined lexically with 'flet, or 'let with #'(lambda ...). If I remember the letoverlambda book correctly, the compiler does not create a symbol object when using 'let, so (symbol-function ...) would not make sense. The let binding is just a stack slot for a pointer to the function object I think.
– Byte
Nov 17 '18 at 13:38
1
True, functions defined withFLET
/LABELS
/LAMBDA
aren't assigned to a symbol (automatically that is, you can of course assign them yourself). For debugging purposes at least on SBCL theFUNCTION-LAMBDA-EXPRESSION
suggested in the answers seems to return a list(FLET <name>)
/(LABELS <name>)
/(LAMBDA <lambda-list>)
for them, but those of course aren't actual function names (these are returned even if the functions are manually assigned to the symbol function slot of a symbol, which demonstrates the "not guaranteed to return anything useful" as sds said).
– jkiiski
Nov 17 '18 at 14:13
add a comment |
Is it possible in common lisp to convert a function to a symbol or a string for further use? What i mean is to get a "+"
or #:|+|
from #'+
.
function common-lisp symbols
Is it possible in common lisp to convert a function to a symbol or a string for further use? What i mean is to get a "+"
or #:|+|
from #'+
.
function common-lisp symbols
function common-lisp symbols
asked Nov 15 '18 at 15:19
amir-tamir-t
1,251725
1,251725
I'm wondering where this need comes from.
– zut
Nov 15 '18 at 20:13
1
@zut In some other languages like Python, function objects have a name attribute, which could be useful some times (example would be implementing multimethods in Python using decorators). Maybe the OP is looking into something similar. Although OP could just wrap it in a cons cell of [function, name], it would be easier to know if CL has such a name attribute already, instead of having to wrap the function object in such an interface. Just guessing though.
– Byte
Nov 17 '18 at 2:51
1
@Byte In CL that would be the other way around. The function is a property of the symbol (the name) rather than the name being a property of the function. If you need both, you should use the symbol and access the function through it.
– jkiiski
Nov 17 '18 at 3:25
@jkiiski Agreed, but I don't think that holds true for function objects defined lexically with 'flet, or 'let with #'(lambda ...). If I remember the letoverlambda book correctly, the compiler does not create a symbol object when using 'let, so (symbol-function ...) would not make sense. The let binding is just a stack slot for a pointer to the function object I think.
– Byte
Nov 17 '18 at 13:38
1
True, functions defined withFLET
/LABELS
/LAMBDA
aren't assigned to a symbol (automatically that is, you can of course assign them yourself). For debugging purposes at least on SBCL theFUNCTION-LAMBDA-EXPRESSION
suggested in the answers seems to return a list(FLET <name>)
/(LABELS <name>)
/(LAMBDA <lambda-list>)
for them, but those of course aren't actual function names (these are returned even if the functions are manually assigned to the symbol function slot of a symbol, which demonstrates the "not guaranteed to return anything useful" as sds said).
– jkiiski
Nov 17 '18 at 14:13
add a comment |
I'm wondering where this need comes from.
– zut
Nov 15 '18 at 20:13
1
@zut In some other languages like Python, function objects have a name attribute, which could be useful some times (example would be implementing multimethods in Python using decorators). Maybe the OP is looking into something similar. Although OP could just wrap it in a cons cell of [function, name], it would be easier to know if CL has such a name attribute already, instead of having to wrap the function object in such an interface. Just guessing though.
– Byte
Nov 17 '18 at 2:51
1
@Byte In CL that would be the other way around. The function is a property of the symbol (the name) rather than the name being a property of the function. If you need both, you should use the symbol and access the function through it.
– jkiiski
Nov 17 '18 at 3:25
@jkiiski Agreed, but I don't think that holds true for function objects defined lexically with 'flet, or 'let with #'(lambda ...). If I remember the letoverlambda book correctly, the compiler does not create a symbol object when using 'let, so (symbol-function ...) would not make sense. The let binding is just a stack slot for a pointer to the function object I think.
– Byte
Nov 17 '18 at 13:38
1
True, functions defined withFLET
/LABELS
/LAMBDA
aren't assigned to a symbol (automatically that is, you can of course assign them yourself). For debugging purposes at least on SBCL theFUNCTION-LAMBDA-EXPRESSION
suggested in the answers seems to return a list(FLET <name>)
/(LABELS <name>)
/(LAMBDA <lambda-list>)
for them, but those of course aren't actual function names (these are returned even if the functions are manually assigned to the symbol function slot of a symbol, which demonstrates the "not guaranteed to return anything useful" as sds said).
– jkiiski
Nov 17 '18 at 14:13
I'm wondering where this need comes from.
– zut
Nov 15 '18 at 20:13
I'm wondering where this need comes from.
– zut
Nov 15 '18 at 20:13
1
1
@zut In some other languages like Python, function objects have a name attribute, which could be useful some times (example would be implementing multimethods in Python using decorators). Maybe the OP is looking into something similar. Although OP could just wrap it in a cons cell of [function, name], it would be easier to know if CL has such a name attribute already, instead of having to wrap the function object in such an interface. Just guessing though.
– Byte
Nov 17 '18 at 2:51
@zut In some other languages like Python, function objects have a name attribute, which could be useful some times (example would be implementing multimethods in Python using decorators). Maybe the OP is looking into something similar. Although OP could just wrap it in a cons cell of [function, name], it would be easier to know if CL has such a name attribute already, instead of having to wrap the function object in such an interface. Just guessing though.
– Byte
Nov 17 '18 at 2:51
1
1
@Byte In CL that would be the other way around. The function is a property of the symbol (the name) rather than the name being a property of the function. If you need both, you should use the symbol and access the function through it.
– jkiiski
Nov 17 '18 at 3:25
@Byte In CL that would be the other way around. The function is a property of the symbol (the name) rather than the name being a property of the function. If you need both, you should use the symbol and access the function through it.
– jkiiski
Nov 17 '18 at 3:25
@jkiiski Agreed, but I don't think that holds true for function objects defined lexically with 'flet, or 'let with #'(lambda ...). If I remember the letoverlambda book correctly, the compiler does not create a symbol object when using 'let, so (symbol-function ...) would not make sense. The let binding is just a stack slot for a pointer to the function object I think.
– Byte
Nov 17 '18 at 13:38
@jkiiski Agreed, but I don't think that holds true for function objects defined lexically with 'flet, or 'let with #'(lambda ...). If I remember the letoverlambda book correctly, the compiler does not create a symbol object when using 'let, so (symbol-function ...) would not make sense. The let binding is just a stack slot for a pointer to the function object I think.
– Byte
Nov 17 '18 at 13:38
1
1
True, functions defined with
FLET
/LABELS
/LAMBDA
aren't assigned to a symbol (automatically that is, you can of course assign them yourself). For debugging purposes at least on SBCL the FUNCTION-LAMBDA-EXPRESSION
suggested in the answers seems to return a list (FLET <name>)
/(LABELS <name>)
/(LAMBDA <lambda-list>)
for them, but those of course aren't actual function names (these are returned even if the functions are manually assigned to the symbol function slot of a symbol, which demonstrates the "not guaranteed to return anything useful" as sds said).– jkiiski
Nov 17 '18 at 14:13
True, functions defined with
FLET
/LABELS
/LAMBDA
aren't assigned to a symbol (automatically that is, you can of course assign them yourself). For debugging purposes at least on SBCL the FUNCTION-LAMBDA-EXPRESSION
suggested in the answers seems to return a list (FLET <name>)
/(LABELS <name>)
/(LAMBDA <lambda-list>)
for them, but those of course aren't actual function names (these are returned even if the functions are manually assigned to the symbol function slot of a symbol, which demonstrates the "not guaranteed to return anything useful" as sds said).– jkiiski
Nov 17 '18 at 14:13
add a comment |
2 Answers
2
active
oldest
votes
The only standard way is
function-lambda-expression
which is not guaranteed to return anything useful.
Neverless, both CLISP and SBCL return the actual function name:
(nth-value 2 (function-lambda-expression #'+))
==> +
or, if you wish,
(symbol-name (nth-value 2 (function-lambda-expression #'+)))
==> "+"
add a comment |
CL-USER> (nth-value 2 (function-lambda-expression #'sin))
SIN
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322578%2fconvert-a-function-to-a-string-or-symbol-in-common-lisp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The only standard way is
function-lambda-expression
which is not guaranteed to return anything useful.
Neverless, both CLISP and SBCL return the actual function name:
(nth-value 2 (function-lambda-expression #'+))
==> +
or, if you wish,
(symbol-name (nth-value 2 (function-lambda-expression #'+)))
==> "+"
add a comment |
The only standard way is
function-lambda-expression
which is not guaranteed to return anything useful.
Neverless, both CLISP and SBCL return the actual function name:
(nth-value 2 (function-lambda-expression #'+))
==> +
or, if you wish,
(symbol-name (nth-value 2 (function-lambda-expression #'+)))
==> "+"
add a comment |
The only standard way is
function-lambda-expression
which is not guaranteed to return anything useful.
Neverless, both CLISP and SBCL return the actual function name:
(nth-value 2 (function-lambda-expression #'+))
==> +
or, if you wish,
(symbol-name (nth-value 2 (function-lambda-expression #'+)))
==> "+"
The only standard way is
function-lambda-expression
which is not guaranteed to return anything useful.
Neverless, both CLISP and SBCL return the actual function name:
(nth-value 2 (function-lambda-expression #'+))
==> +
or, if you wish,
(symbol-name (nth-value 2 (function-lambda-expression #'+)))
==> "+"
answered Nov 15 '18 at 16:12
sdssds
40k1497175
40k1497175
add a comment |
add a comment |
CL-USER> (nth-value 2 (function-lambda-expression #'sin))
SIN
add a comment |
CL-USER> (nth-value 2 (function-lambda-expression #'sin))
SIN
add a comment |
CL-USER> (nth-value 2 (function-lambda-expression #'sin))
SIN
CL-USER> (nth-value 2 (function-lambda-expression #'sin))
SIN
answered Nov 15 '18 at 15:58
Rainer JoswigRainer Joswig
112k8169287
112k8169287
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322578%2fconvert-a-function-to-a-string-or-symbol-in-common-lisp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm wondering where this need comes from.
– zut
Nov 15 '18 at 20:13
1
@zut In some other languages like Python, function objects have a name attribute, which could be useful some times (example would be implementing multimethods in Python using decorators). Maybe the OP is looking into something similar. Although OP could just wrap it in a cons cell of [function, name], it would be easier to know if CL has such a name attribute already, instead of having to wrap the function object in such an interface. Just guessing though.
– Byte
Nov 17 '18 at 2:51
1
@Byte In CL that would be the other way around. The function is a property of the symbol (the name) rather than the name being a property of the function. If you need both, you should use the symbol and access the function through it.
– jkiiski
Nov 17 '18 at 3:25
@jkiiski Agreed, but I don't think that holds true for function objects defined lexically with 'flet, or 'let with #'(lambda ...). If I remember the letoverlambda book correctly, the compiler does not create a symbol object when using 'let, so (symbol-function ...) would not make sense. The let binding is just a stack slot for a pointer to the function object I think.
– Byte
Nov 17 '18 at 13:38
1
True, functions defined with
FLET
/LABELS
/LAMBDA
aren't assigned to a symbol (automatically that is, you can of course assign them yourself). For debugging purposes at least on SBCL theFUNCTION-LAMBDA-EXPRESSION
suggested in the answers seems to return a list(FLET <name>)
/(LABELS <name>)
/(LAMBDA <lambda-list>)
for them, but those of course aren't actual function names (these are returned even if the functions are manually assigned to the symbol function slot of a symbol, which demonstrates the "not guaranteed to return anything useful" as sds said).– jkiiski
Nov 17 '18 at 14:13