Confusing Lisp syntax with (operator-integer-operator) format
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
add a comment |
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
lispology.com/show?JIH
– Rainer Joswig
Nov 26 '18 at 21:18
add a comment |
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
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
lisp common-lisp
edited Nov 26 '18 at 21:46
Rainer Joswig
112k8169290
112k8169290
asked Nov 26 '18 at 8:15
AJ GoudelAJ Goudel
3718
3718
lispology.com/show?JIH
– Rainer Joswig
Nov 26 '18 at 21:18
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
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%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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 26 '18 at 9:14
coredump
22.2k43047
22.2k43047
answered Nov 26 '18 at 8:27
RenzoRenzo
18.1k43144
18.1k43144
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 26 '18 at 21:46
answered Nov 26 '18 at 21:40
Rainer JoswigRainer Joswig
112k8169290
112k8169290
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%2f53477007%2fconfusing-lisp-syntax-with-operator-integer-operator-format%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
lispology.com/show?JIH
– Rainer Joswig
Nov 26 '18 at 21:18