Can a scheme function return a global symbol and its value?











up vote
2
down vote

favorite












Is it possible to write the scheme function symtab below?



(define x 1)
(define y 2)
(symtab x) ; => (x . 1)
(symtab y) ; => (y . 2)


If it isn't possible, can a similar function be defined which has quoted arguments?



(symtab 'x) ; => (x . 1)
(symtab 'y) ; => (y . 2)









share|improve this question


























    up vote
    2
    down vote

    favorite












    Is it possible to write the scheme function symtab below?



    (define x 1)
    (define y 2)
    (symtab x) ; => (x . 1)
    (symtab y) ; => (y . 2)


    If it isn't possible, can a similar function be defined which has quoted arguments?



    (symtab 'x) ; => (x . 1)
    (symtab 'y) ; => (y . 2)









    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Is it possible to write the scheme function symtab below?



      (define x 1)
      (define y 2)
      (symtab x) ; => (x . 1)
      (symtab y) ; => (y . 2)


      If it isn't possible, can a similar function be defined which has quoted arguments?



      (symtab 'x) ; => (x . 1)
      (symtab 'y) ; => (y . 2)









      share|improve this question













      Is it possible to write the scheme function symtab below?



      (define x 1)
      (define y 2)
      (symtab x) ; => (x . 1)
      (symtab y) ; => (y . 2)


      If it isn't possible, can a similar function be defined which has quoted arguments?



      (symtab 'x) ; => (x . 1)
      (symtab 'y) ; => (y . 2)






      scheme






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 16:46









      cfgauss

      154




      154
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          It's possible with quoted arguments:



          (define (symtab s)
          (cons s (eval s (interaction-environment))))


          Now lets test it:



          (define xy 12)

          (symtab 'xy)
          => (xy . 12)


          EDIT: However it's not very reliable (using macro too) - please read closely related Why isn't there an unquote Lisp primitive?



          It's because Scheme like most languages is lexically scoped. It means that variable names are resolved when function is defined, as opposed to dynamic scope when variable names are resolved at run-time. So



          (define xy 12)

          (define (getXY) xy)

          (getXY)
          => 12


          This code works, because when we define getXY, name xy is known and can be resolved to top-level xy. However



          (define (getXY) xy)

          (let ((xy 21))
          (getXY))
          => Unbound variable: xy


          This doesn't work, because when getXY was defined, xy was not known (and my Guile Scheme also gave me warning when defining getXY "warning: possibly unbound variable `xy'").



          It will work in Emacs Lisp - dynamically scoped.






          share|improve this answer






























            up vote
            1
            down vote













            Yes, with macros.



            > (define-syntax symtab
            (syntax-rules ()
            ((_ x)
            (cons 'x x))))

            > (define x 1)
            > (define y 2)
            > (symtab x)
            '(x . 1)
            > (symtab y)
            '(y . 2)
            >


            A macro is not actually a function (so technically the essential answer to your question is no). With mere functions your task might even be impossible in most languages. But Scheme provides us with macros that allow us to manipulate a program from within itself. For more on macros and how they are different from functions I recommend taking a look at the following tutorial: http://www.greghendershott.com/fear-of-macros/all.html#%28part..Transform%29



            It is based on Racket, but it is substantially applicable across various Scheme dialects. Enjoy!






            share|improve this answer























            • To clarify, it's not possible to create a Scheme function which works with unquoted arguments but it is possible to create a Scheme macro (your example). And it's also possible to create a Scheme function which works with quoted arguments (rsm's example). Is that correct?
              – cfgauss
              Nov 20 at 3:29










            • yes, that's correct. it can't be done using functions, because arguments get evaluated (so you can't get argument original name, value only). it can be done with macro, but it's not very reliable in either case - please read closely related Why isn't there an unquote Lisp primitive?
              – rsm
              Nov 20 at 11:16










            • @rsm Could you give examples of the unreliability of each of the two solutions?
              – cfgauss
              Nov 21 at 0:39










            • @cfgauss Hi, please check my updated answer. Hope it's clear now.
              – rsm
              Nov 21 at 10:03











            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',
            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%2f53379193%2fcan-a-scheme-function-return-a-global-symbol-and-its-value%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








            up vote
            1
            down vote



            accepted










            It's possible with quoted arguments:



            (define (symtab s)
            (cons s (eval s (interaction-environment))))


            Now lets test it:



            (define xy 12)

            (symtab 'xy)
            => (xy . 12)


            EDIT: However it's not very reliable (using macro too) - please read closely related Why isn't there an unquote Lisp primitive?



            It's because Scheme like most languages is lexically scoped. It means that variable names are resolved when function is defined, as opposed to dynamic scope when variable names are resolved at run-time. So



            (define xy 12)

            (define (getXY) xy)

            (getXY)
            => 12


            This code works, because when we define getXY, name xy is known and can be resolved to top-level xy. However



            (define (getXY) xy)

            (let ((xy 21))
            (getXY))
            => Unbound variable: xy


            This doesn't work, because when getXY was defined, xy was not known (and my Guile Scheme also gave me warning when defining getXY "warning: possibly unbound variable `xy'").



            It will work in Emacs Lisp - dynamically scoped.






            share|improve this answer



























              up vote
              1
              down vote



              accepted










              It's possible with quoted arguments:



              (define (symtab s)
              (cons s (eval s (interaction-environment))))


              Now lets test it:



              (define xy 12)

              (symtab 'xy)
              => (xy . 12)


              EDIT: However it's not very reliable (using macro too) - please read closely related Why isn't there an unquote Lisp primitive?



              It's because Scheme like most languages is lexically scoped. It means that variable names are resolved when function is defined, as opposed to dynamic scope when variable names are resolved at run-time. So



              (define xy 12)

              (define (getXY) xy)

              (getXY)
              => 12


              This code works, because when we define getXY, name xy is known and can be resolved to top-level xy. However



              (define (getXY) xy)

              (let ((xy 21))
              (getXY))
              => Unbound variable: xy


              This doesn't work, because when getXY was defined, xy was not known (and my Guile Scheme also gave me warning when defining getXY "warning: possibly unbound variable `xy'").



              It will work in Emacs Lisp - dynamically scoped.






              share|improve this answer

























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                It's possible with quoted arguments:



                (define (symtab s)
                (cons s (eval s (interaction-environment))))


                Now lets test it:



                (define xy 12)

                (symtab 'xy)
                => (xy . 12)


                EDIT: However it's not very reliable (using macro too) - please read closely related Why isn't there an unquote Lisp primitive?



                It's because Scheme like most languages is lexically scoped. It means that variable names are resolved when function is defined, as opposed to dynamic scope when variable names are resolved at run-time. So



                (define xy 12)

                (define (getXY) xy)

                (getXY)
                => 12


                This code works, because when we define getXY, name xy is known and can be resolved to top-level xy. However



                (define (getXY) xy)

                (let ((xy 21))
                (getXY))
                => Unbound variable: xy


                This doesn't work, because when getXY was defined, xy was not known (and my Guile Scheme also gave me warning when defining getXY "warning: possibly unbound variable `xy'").



                It will work in Emacs Lisp - dynamically scoped.






                share|improve this answer














                It's possible with quoted arguments:



                (define (symtab s)
                (cons s (eval s (interaction-environment))))


                Now lets test it:



                (define xy 12)

                (symtab 'xy)
                => (xy . 12)


                EDIT: However it's not very reliable (using macro too) - please read closely related Why isn't there an unquote Lisp primitive?



                It's because Scheme like most languages is lexically scoped. It means that variable names are resolved when function is defined, as opposed to dynamic scope when variable names are resolved at run-time. So



                (define xy 12)

                (define (getXY) xy)

                (getXY)
                => 12


                This code works, because when we define getXY, name xy is known and can be resolved to top-level xy. However



                (define (getXY) xy)

                (let ((xy 21))
                (getXY))
                => Unbound variable: xy


                This doesn't work, because when getXY was defined, xy was not known (and my Guile Scheme also gave me warning when defining getXY "warning: possibly unbound variable `xy'").



                It will work in Emacs Lisp - dynamically scoped.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 at 10:01

























                answered Nov 19 at 20:39









                rsm

                1,12131421




                1,12131421
























                    up vote
                    1
                    down vote













                    Yes, with macros.



                    > (define-syntax symtab
                    (syntax-rules ()
                    ((_ x)
                    (cons 'x x))))

                    > (define x 1)
                    > (define y 2)
                    > (symtab x)
                    '(x . 1)
                    > (symtab y)
                    '(y . 2)
                    >


                    A macro is not actually a function (so technically the essential answer to your question is no). With mere functions your task might even be impossible in most languages. But Scheme provides us with macros that allow us to manipulate a program from within itself. For more on macros and how they are different from functions I recommend taking a look at the following tutorial: http://www.greghendershott.com/fear-of-macros/all.html#%28part..Transform%29



                    It is based on Racket, but it is substantially applicable across various Scheme dialects. Enjoy!






                    share|improve this answer























                    • To clarify, it's not possible to create a Scheme function which works with unquoted arguments but it is possible to create a Scheme macro (your example). And it's also possible to create a Scheme function which works with quoted arguments (rsm's example). Is that correct?
                      – cfgauss
                      Nov 20 at 3:29










                    • yes, that's correct. it can't be done using functions, because arguments get evaluated (so you can't get argument original name, value only). it can be done with macro, but it's not very reliable in either case - please read closely related Why isn't there an unquote Lisp primitive?
                      – rsm
                      Nov 20 at 11:16










                    • @rsm Could you give examples of the unreliability of each of the two solutions?
                      – cfgauss
                      Nov 21 at 0:39










                    • @cfgauss Hi, please check my updated answer. Hope it's clear now.
                      – rsm
                      Nov 21 at 10:03















                    up vote
                    1
                    down vote













                    Yes, with macros.



                    > (define-syntax symtab
                    (syntax-rules ()
                    ((_ x)
                    (cons 'x x))))

                    > (define x 1)
                    > (define y 2)
                    > (symtab x)
                    '(x . 1)
                    > (symtab y)
                    '(y . 2)
                    >


                    A macro is not actually a function (so technically the essential answer to your question is no). With mere functions your task might even be impossible in most languages. But Scheme provides us with macros that allow us to manipulate a program from within itself. For more on macros and how they are different from functions I recommend taking a look at the following tutorial: http://www.greghendershott.com/fear-of-macros/all.html#%28part..Transform%29



                    It is based on Racket, but it is substantially applicable across various Scheme dialects. Enjoy!






                    share|improve this answer























                    • To clarify, it's not possible to create a Scheme function which works with unquoted arguments but it is possible to create a Scheme macro (your example). And it's also possible to create a Scheme function which works with quoted arguments (rsm's example). Is that correct?
                      – cfgauss
                      Nov 20 at 3:29










                    • yes, that's correct. it can't be done using functions, because arguments get evaluated (so you can't get argument original name, value only). it can be done with macro, but it's not very reliable in either case - please read closely related Why isn't there an unquote Lisp primitive?
                      – rsm
                      Nov 20 at 11:16










                    • @rsm Could you give examples of the unreliability of each of the two solutions?
                      – cfgauss
                      Nov 21 at 0:39










                    • @cfgauss Hi, please check my updated answer. Hope it's clear now.
                      – rsm
                      Nov 21 at 10:03













                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Yes, with macros.



                    > (define-syntax symtab
                    (syntax-rules ()
                    ((_ x)
                    (cons 'x x))))

                    > (define x 1)
                    > (define y 2)
                    > (symtab x)
                    '(x . 1)
                    > (symtab y)
                    '(y . 2)
                    >


                    A macro is not actually a function (so technically the essential answer to your question is no). With mere functions your task might even be impossible in most languages. But Scheme provides us with macros that allow us to manipulate a program from within itself. For more on macros and how they are different from functions I recommend taking a look at the following tutorial: http://www.greghendershott.com/fear-of-macros/all.html#%28part..Transform%29



                    It is based on Racket, but it is substantially applicable across various Scheme dialects. Enjoy!






                    share|improve this answer














                    Yes, with macros.



                    > (define-syntax symtab
                    (syntax-rules ()
                    ((_ x)
                    (cons 'x x))))

                    > (define x 1)
                    > (define y 2)
                    > (symtab x)
                    '(x . 1)
                    > (symtab y)
                    '(y . 2)
                    >


                    A macro is not actually a function (so technically the essential answer to your question is no). With mere functions your task might even be impossible in most languages. But Scheme provides us with macros that allow us to manipulate a program from within itself. For more on macros and how they are different from functions I recommend taking a look at the following tutorial: http://www.greghendershott.com/fear-of-macros/all.html#%28part..Transform%29



                    It is based on Racket, but it is substantially applicable across various Scheme dialects. Enjoy!







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 19 at 19:09

























                    answered Nov 19 at 18:10









                    Alejandro Alvarado

                    739




                    739












                    • To clarify, it's not possible to create a Scheme function which works with unquoted arguments but it is possible to create a Scheme macro (your example). And it's also possible to create a Scheme function which works with quoted arguments (rsm's example). Is that correct?
                      – cfgauss
                      Nov 20 at 3:29










                    • yes, that's correct. it can't be done using functions, because arguments get evaluated (so you can't get argument original name, value only). it can be done with macro, but it's not very reliable in either case - please read closely related Why isn't there an unquote Lisp primitive?
                      – rsm
                      Nov 20 at 11:16










                    • @rsm Could you give examples of the unreliability of each of the two solutions?
                      – cfgauss
                      Nov 21 at 0:39










                    • @cfgauss Hi, please check my updated answer. Hope it's clear now.
                      – rsm
                      Nov 21 at 10:03


















                    • To clarify, it's not possible to create a Scheme function which works with unquoted arguments but it is possible to create a Scheme macro (your example). And it's also possible to create a Scheme function which works with quoted arguments (rsm's example). Is that correct?
                      – cfgauss
                      Nov 20 at 3:29










                    • yes, that's correct. it can't be done using functions, because arguments get evaluated (so you can't get argument original name, value only). it can be done with macro, but it's not very reliable in either case - please read closely related Why isn't there an unquote Lisp primitive?
                      – rsm
                      Nov 20 at 11:16










                    • @rsm Could you give examples of the unreliability of each of the two solutions?
                      – cfgauss
                      Nov 21 at 0:39










                    • @cfgauss Hi, please check my updated answer. Hope it's clear now.
                      – rsm
                      Nov 21 at 10:03
















                    To clarify, it's not possible to create a Scheme function which works with unquoted arguments but it is possible to create a Scheme macro (your example). And it's also possible to create a Scheme function which works with quoted arguments (rsm's example). Is that correct?
                    – cfgauss
                    Nov 20 at 3:29




                    To clarify, it's not possible to create a Scheme function which works with unquoted arguments but it is possible to create a Scheme macro (your example). And it's also possible to create a Scheme function which works with quoted arguments (rsm's example). Is that correct?
                    – cfgauss
                    Nov 20 at 3:29












                    yes, that's correct. it can't be done using functions, because arguments get evaluated (so you can't get argument original name, value only). it can be done with macro, but it's not very reliable in either case - please read closely related Why isn't there an unquote Lisp primitive?
                    – rsm
                    Nov 20 at 11:16




                    yes, that's correct. it can't be done using functions, because arguments get evaluated (so you can't get argument original name, value only). it can be done with macro, but it's not very reliable in either case - please read closely related Why isn't there an unquote Lisp primitive?
                    – rsm
                    Nov 20 at 11:16












                    @rsm Could you give examples of the unreliability of each of the two solutions?
                    – cfgauss
                    Nov 21 at 0:39




                    @rsm Could you give examples of the unreliability of each of the two solutions?
                    – cfgauss
                    Nov 21 at 0:39












                    @cfgauss Hi, please check my updated answer. Hope it's clear now.
                    – rsm
                    Nov 21 at 10:03




                    @cfgauss Hi, please check my updated answer. Hope it's clear now.
                    – rsm
                    Nov 21 at 10:03


















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379193%2fcan-a-scheme-function-return-a-global-symbol-and-its-value%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

                    Feedback on college project

                    Futebolista

                    Albești (Vaslui)