Convert a FUNCTION to a STRING or SYMBOL in Common Lisp












4















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 #'+.










share|improve this question























  • 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 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
















4















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 #'+.










share|improve this question























  • 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 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














4












4








4








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 #'+.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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 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

















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












2 Answers
2






active

oldest

votes


















5














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 #'+)))
==> "+"





share|improve this answer































    3














    CL-USER> (nth-value 2 (function-lambda-expression #'sin))
    SIN





    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      5














      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 #'+)))
      ==> "+"





      share|improve this answer




























        5














        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 #'+)))
        ==> "+"





        share|improve this answer


























          5












          5








          5







          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 #'+)))
          ==> "+"





          share|improve this answer













          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 #'+)))
          ==> "+"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 16:12









          sdssds

          40k1497175




          40k1497175

























              3














              CL-USER> (nth-value 2 (function-lambda-expression #'sin))
              SIN





              share|improve this answer




























                3














                CL-USER> (nth-value 2 (function-lambda-expression #'sin))
                SIN





                share|improve this answer


























                  3












                  3








                  3







                  CL-USER> (nth-value 2 (function-lambda-expression #'sin))
                  SIN





                  share|improve this answer













                  CL-USER> (nth-value 2 (function-lambda-expression #'sin))
                  SIN






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 15:58









                  Rainer JoswigRainer Joswig

                  112k8169287




                  112k8169287






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values