Confusing Lisp syntax with (operator-integer-operator) format












1















I am new to lisp and I had a question about this LISP syntax:



(defparameter *binary-operators*
'((+ 1 +) (- 1 -) (* 2 *)
(x 2 *) (/ 2 %) (^ 3 expt)))


From what I understand, defparameter allows the binary operators variable to be reassigned but I am confused as to how the (+ 1 +), (- 1 -) ... are evaluated. I know in LISP that (+ 4 6) would result in (4 + 6) = 10 but the same logic would result in (1 + +) which does not make sense. What does the above syntax represent?










share|improve this question

























  • lispology.com/show?JIH

    – Rainer Joswig
    Nov 26 '18 at 21:18
















1















I am new to lisp and I had a question about this LISP syntax:



(defparameter *binary-operators*
'((+ 1 +) (- 1 -) (* 2 *)
(x 2 *) (/ 2 %) (^ 3 expt)))


From what I understand, defparameter allows the binary operators variable to be reassigned but I am confused as to how the (+ 1 +), (- 1 -) ... are evaluated. I know in LISP that (+ 4 6) would result in (4 + 6) = 10 but the same logic would result in (1 + +) which does not make sense. What does the above syntax represent?










share|improve this question

























  • lispology.com/show?JIH

    – Rainer Joswig
    Nov 26 '18 at 21:18














1












1








1








I am new to lisp and I had a question about this LISP syntax:



(defparameter *binary-operators*
'((+ 1 +) (- 1 -) (* 2 *)
(x 2 *) (/ 2 %) (^ 3 expt)))


From what I understand, defparameter allows the binary operators variable to be reassigned but I am confused as to how the (+ 1 +), (- 1 -) ... are evaluated. I know in LISP that (+ 4 6) would result in (4 + 6) = 10 but the same logic would result in (1 + +) which does not make sense. What does the above syntax represent?










share|improve this question
















I am new to lisp and I had a question about this LISP syntax:



(defparameter *binary-operators*
'((+ 1 +) (- 1 -) (* 2 *)
(x 2 *) (/ 2 %) (^ 3 expt)))


From what I understand, defparameter allows the binary operators variable to be reassigned but I am confused as to how the (+ 1 +), (- 1 -) ... are evaluated. I know in LISP that (+ 4 6) would result in (4 + 6) = 10 but the same logic would result in (1 + +) which does not make sense. What does the above syntax represent?







lisp common-lisp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 21:46









Rainer Joswig

111k8167284




111k8167284










asked Nov 26 '18 at 8:15









AJ GoudelAJ Goudel

3517




3517













  • lispology.com/show?JIH

    – Rainer Joswig
    Nov 26 '18 at 21:18



















  • lispology.com/show?JIH

    – Rainer Joswig
    Nov 26 '18 at 21:18

















lispology.com/show?JIH

– Rainer Joswig
Nov 26 '18 at 21:18





lispology.com/show?JIH

– Rainer Joswig
Nov 26 '18 at 21:18












2 Answers
2






active

oldest

votes


















6














In Common Lisp,



(defparameter name initial-value)


(see the manual) introduces a new special (global) variable name with a new value, given by evaluating initial-value.



So, in the example above, the special variable *binary-operators* is assigned a list of triples, each of them constitued by a symbol, a number, and another symbol. In other words, it assigns some data to a variable, and not, as you were thinking, redefines the syntax of the language.



Guessing from the values present in the list, this seems a variable that is assigned a list of arithmetic operators, each of them with the priority, and with the equivalent Common Lisp operator/function. Maybe this is a line of some program that maps arithmetic expressions in lisp s-expressions, or something like that.






share|improve this answer

































    4














    Lisp: lists and symbols are used in data and in code



    This is one of the applications where code is data. In Lisp symbols and lists are data. But they are also used to write programs: the symbols then are used for variable names, function names and much more. Lists are used to write expressions in the Lisp language - these are called forms.



    In a Lisp program



    (+ 1 2)


    is a function call of the function named + with two values.



    '(+ 1 2)


    or



    (quote (+ 1 2))


    then is data -> the list of the symbol + and the numbers 1 and 2.



    Example: infix to prefix conversion



    The form you used defines a mapping from a symbol denoting a mathematical function to the weight and the actual Lisp function it represents for the conversion.



    See Lispology: Infix to prefix



    (defparameter *binary-operators*
    ; operator weight Lisp function
    '((+ 1 +)
    (- 1 -)
    (* 2 *)
    (x 2 *)
    (/ 2 %)
    (^ 3 expt)))


    We can use it to convert infix mathematical expressions to prefix Lisp expressions (see the above linked article for the code):



    CL-USER 52 > (infix-prefix '(2 * 3 ^ 4))
    (* 2 (EXPT 3 4))


    When we change that assoc list, then the conversion will be different. Let's change the weight of the ^ operator:



    CL-USER 53 > (defparameter *binary-operators*
    '((+ 1 +)
    (- 1 -)
    (* 2 *)
    (x 2 *)
    (/ 2 %)
    (^ 1 expt))) ; weight changed to 1
    *BINARY-OPERATORS*


    Now we can convert the example from above and we get a different Lisp form:



    CL-USER 54 > (infix-prefix '(2 * 3 ^ 4))
    (EXPT (* 2 3) 4)


    So, *binary-operators* is data, which drives the conversion from infix mathematical expressions to Lisp forms. Instead of hardwiring the rules into the code, here we are using an assoc list to keep the mappings. Thus allows us to add new operators by changing the assoc list, without changing the actual code.






    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%2f53477007%2fconfusing-lisp-syntax-with-operator-integer-operator-format%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









      6














      In Common Lisp,



      (defparameter name initial-value)


      (see the manual) introduces a new special (global) variable name with a new value, given by evaluating initial-value.



      So, in the example above, the special variable *binary-operators* is assigned a list of triples, each of them constitued by a symbol, a number, and another symbol. In other words, it assigns some data to a variable, and not, as you were thinking, redefines the syntax of the language.



      Guessing from the values present in the list, this seems a variable that is assigned a list of arithmetic operators, each of them with the priority, and with the equivalent Common Lisp operator/function. Maybe this is a line of some program that maps arithmetic expressions in lisp s-expressions, or something like that.






      share|improve this answer






























        6














        In Common Lisp,



        (defparameter name initial-value)


        (see the manual) introduces a new special (global) variable name with a new value, given by evaluating initial-value.



        So, in the example above, the special variable *binary-operators* is assigned a list of triples, each of them constitued by a symbol, a number, and another symbol. In other words, it assigns some data to a variable, and not, as you were thinking, redefines the syntax of the language.



        Guessing from the values present in the list, this seems a variable that is assigned a list of arithmetic operators, each of them with the priority, and with the equivalent Common Lisp operator/function. Maybe this is a line of some program that maps arithmetic expressions in lisp s-expressions, or something like that.






        share|improve this answer




























          6












          6








          6







          In Common Lisp,



          (defparameter name initial-value)


          (see the manual) introduces a new special (global) variable name with a new value, given by evaluating initial-value.



          So, in the example above, the special variable *binary-operators* is assigned a list of triples, each of them constitued by a symbol, a number, and another symbol. In other words, it assigns some data to a variable, and not, as you were thinking, redefines the syntax of the language.



          Guessing from the values present in the list, this seems a variable that is assigned a list of arithmetic operators, each of them with the priority, and with the equivalent Common Lisp operator/function. Maybe this is a line of some program that maps arithmetic expressions in lisp s-expressions, or something like that.






          share|improve this answer















          In Common Lisp,



          (defparameter name initial-value)


          (see the manual) introduces a new special (global) variable name with a new value, given by evaluating initial-value.



          So, in the example above, the special variable *binary-operators* is assigned a list of triples, each of them constitued by a symbol, a number, and another symbol. In other words, it assigns some data to a variable, and not, as you were thinking, redefines the syntax of the language.



          Guessing from the values present in the list, this seems a variable that is assigned a list of arithmetic operators, each of them with the priority, and with the equivalent Common Lisp operator/function. Maybe this is a line of some program that maps arithmetic expressions in lisp s-expressions, or something like that.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 9:14









          coredump

          21.4k33047




          21.4k33047










          answered Nov 26 '18 at 8:27









          RenzoRenzo

          17.5k43043




          17.5k43043

























              4














              Lisp: lists and symbols are used in data and in code



              This is one of the applications where code is data. In Lisp symbols and lists are data. But they are also used to write programs: the symbols then are used for variable names, function names and much more. Lists are used to write expressions in the Lisp language - these are called forms.



              In a Lisp program



              (+ 1 2)


              is a function call of the function named + with two values.



              '(+ 1 2)


              or



              (quote (+ 1 2))


              then is data -> the list of the symbol + and the numbers 1 and 2.



              Example: infix to prefix conversion



              The form you used defines a mapping from a symbol denoting a mathematical function to the weight and the actual Lisp function it represents for the conversion.



              See Lispology: Infix to prefix



              (defparameter *binary-operators*
              ; operator weight Lisp function
              '((+ 1 +)
              (- 1 -)
              (* 2 *)
              (x 2 *)
              (/ 2 %)
              (^ 3 expt)))


              We can use it to convert infix mathematical expressions to prefix Lisp expressions (see the above linked article for the code):



              CL-USER 52 > (infix-prefix '(2 * 3 ^ 4))
              (* 2 (EXPT 3 4))


              When we change that assoc list, then the conversion will be different. Let's change the weight of the ^ operator:



              CL-USER 53 > (defparameter *binary-operators*
              '((+ 1 +)
              (- 1 -)
              (* 2 *)
              (x 2 *)
              (/ 2 %)
              (^ 1 expt))) ; weight changed to 1
              *BINARY-OPERATORS*


              Now we can convert the example from above and we get a different Lisp form:



              CL-USER 54 > (infix-prefix '(2 * 3 ^ 4))
              (EXPT (* 2 3) 4)


              So, *binary-operators* is data, which drives the conversion from infix mathematical expressions to Lisp forms. Instead of hardwiring the rules into the code, here we are using an assoc list to keep the mappings. Thus allows us to add new operators by changing the assoc list, without changing the actual code.






              share|improve this answer






























                4














                Lisp: lists and symbols are used in data and in code



                This is one of the applications where code is data. In Lisp symbols and lists are data. But they are also used to write programs: the symbols then are used for variable names, function names and much more. Lists are used to write expressions in the Lisp language - these are called forms.



                In a Lisp program



                (+ 1 2)


                is a function call of the function named + with two values.



                '(+ 1 2)


                or



                (quote (+ 1 2))


                then is data -> the list of the symbol + and the numbers 1 and 2.



                Example: infix to prefix conversion



                The form you used defines a mapping from a symbol denoting a mathematical function to the weight and the actual Lisp function it represents for the conversion.



                See Lispology: Infix to prefix



                (defparameter *binary-operators*
                ; operator weight Lisp function
                '((+ 1 +)
                (- 1 -)
                (* 2 *)
                (x 2 *)
                (/ 2 %)
                (^ 3 expt)))


                We can use it to convert infix mathematical expressions to prefix Lisp expressions (see the above linked article for the code):



                CL-USER 52 > (infix-prefix '(2 * 3 ^ 4))
                (* 2 (EXPT 3 4))


                When we change that assoc list, then the conversion will be different. Let's change the weight of the ^ operator:



                CL-USER 53 > (defparameter *binary-operators*
                '((+ 1 +)
                (- 1 -)
                (* 2 *)
                (x 2 *)
                (/ 2 %)
                (^ 1 expt))) ; weight changed to 1
                *BINARY-OPERATORS*


                Now we can convert the example from above and we get a different Lisp form:



                CL-USER 54 > (infix-prefix '(2 * 3 ^ 4))
                (EXPT (* 2 3) 4)


                So, *binary-operators* is data, which drives the conversion from infix mathematical expressions to Lisp forms. Instead of hardwiring the rules into the code, here we are using an assoc list to keep the mappings. Thus allows us to add new operators by changing the assoc list, without changing the actual code.






                share|improve this answer




























                  4












                  4








                  4







                  Lisp: lists and symbols are used in data and in code



                  This is one of the applications where code is data. In Lisp symbols and lists are data. But they are also used to write programs: the symbols then are used for variable names, function names and much more. Lists are used to write expressions in the Lisp language - these are called forms.



                  In a Lisp program



                  (+ 1 2)


                  is a function call of the function named + with two values.



                  '(+ 1 2)


                  or



                  (quote (+ 1 2))


                  then is data -> the list of the symbol + and the numbers 1 and 2.



                  Example: infix to prefix conversion



                  The form you used defines a mapping from a symbol denoting a mathematical function to the weight and the actual Lisp function it represents for the conversion.



                  See Lispology: Infix to prefix



                  (defparameter *binary-operators*
                  ; operator weight Lisp function
                  '((+ 1 +)
                  (- 1 -)
                  (* 2 *)
                  (x 2 *)
                  (/ 2 %)
                  (^ 3 expt)))


                  We can use it to convert infix mathematical expressions to prefix Lisp expressions (see the above linked article for the code):



                  CL-USER 52 > (infix-prefix '(2 * 3 ^ 4))
                  (* 2 (EXPT 3 4))


                  When we change that assoc list, then the conversion will be different. Let's change the weight of the ^ operator:



                  CL-USER 53 > (defparameter *binary-operators*
                  '((+ 1 +)
                  (- 1 -)
                  (* 2 *)
                  (x 2 *)
                  (/ 2 %)
                  (^ 1 expt))) ; weight changed to 1
                  *BINARY-OPERATORS*


                  Now we can convert the example from above and we get a different Lisp form:



                  CL-USER 54 > (infix-prefix '(2 * 3 ^ 4))
                  (EXPT (* 2 3) 4)


                  So, *binary-operators* is data, which drives the conversion from infix mathematical expressions to Lisp forms. Instead of hardwiring the rules into the code, here we are using an assoc list to keep the mappings. Thus allows us to add new operators by changing the assoc list, without changing the actual code.






                  share|improve this answer















                  Lisp: lists and symbols are used in data and in code



                  This is one of the applications where code is data. In Lisp symbols and lists are data. But they are also used to write programs: the symbols then are used for variable names, function names and much more. Lists are used to write expressions in the Lisp language - these are called forms.



                  In a Lisp program



                  (+ 1 2)


                  is a function call of the function named + with two values.



                  '(+ 1 2)


                  or



                  (quote (+ 1 2))


                  then is data -> the list of the symbol + and the numbers 1 and 2.



                  Example: infix to prefix conversion



                  The form you used defines a mapping from a symbol denoting a mathematical function to the weight and the actual Lisp function it represents for the conversion.



                  See Lispology: Infix to prefix



                  (defparameter *binary-operators*
                  ; operator weight Lisp function
                  '((+ 1 +)
                  (- 1 -)
                  (* 2 *)
                  (x 2 *)
                  (/ 2 %)
                  (^ 3 expt)))


                  We can use it to convert infix mathematical expressions to prefix Lisp expressions (see the above linked article for the code):



                  CL-USER 52 > (infix-prefix '(2 * 3 ^ 4))
                  (* 2 (EXPT 3 4))


                  When we change that assoc list, then the conversion will be different. Let's change the weight of the ^ operator:



                  CL-USER 53 > (defparameter *binary-operators*
                  '((+ 1 +)
                  (- 1 -)
                  (* 2 *)
                  (x 2 *)
                  (/ 2 %)
                  (^ 1 expt))) ; weight changed to 1
                  *BINARY-OPERATORS*


                  Now we can convert the example from above and we get a different Lisp form:



                  CL-USER 54 > (infix-prefix '(2 * 3 ^ 4))
                  (EXPT (* 2 3) 4)


                  So, *binary-operators* is data, which drives the conversion from infix mathematical expressions to Lisp forms. Instead of hardwiring the rules into the code, here we are using an assoc list to keep the mappings. Thus allows us to add new operators by changing the assoc list, without changing the actual code.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 26 '18 at 21:46

























                  answered Nov 26 '18 at 21:40









                  Rainer JoswigRainer Joswig

                  111k8167284




                  111k8167284






























                      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%2f53477007%2fconfusing-lisp-syntax-with-operator-integer-operator-format%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

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks