Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am wondering whether there is a built-in way to do a defensive copy of a values list type in Common Lisp. It is my understanding that this list is not a regular list, but is some reserved type.
I suppose one solution is to convert to a proper list, make a copy of that and then convert it back to the values list. However, I am unsure of the most efficient way to do that.
Any help is appreciated.
The entire purpose and effect of the multiple values mechanism in Common Lisp is to allow transferring more than one value back from a form without consing, i. e. without wrapping another temporary object around them.
Under the hood, you can imagine those values to reside directly on the stack or even in registers.
For example, if you have such a function:
(defun foo ()
(values 1 2))
and you call it like this:
(multiple-value-bind (a b) (foo)
(+ a b))
then 1 gets assigned to a and 2 to b directly, without first putting anything into any kind of intermediate structure.
There is thus no such thing as a values object, nor a single place to hold values, so there can also be no type or anything like that associated with it.
I don't see how a “defensive” copy might be needed, but you can wrap things received as values into a list using multiple-value-list, return things in a list as multiple values using values-list, or set multiple places to the values returned from some form using multiple-value-setq or (setf values).
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
I am looking for the best way to call a function in powershell.
I search on google and find the two below ways to call a function. Ref - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-5.1
func -Size 50 -Length 100
func 50 100
Note - Assume function with name 'func' and 2 params ($Size, $Length)
is there any different way other than mentioned above to call a function in PowerShell ?
As commented by #Santiago:
Both ways are valid, there is no "better way" except for named parameters being more verbose could help others get a better understanding of what your function is doing
The best practice (for this particular question) is actually included in PSScriptAnalyzer that comes standard with e.g. the PowerShell Extension for Visual Studio Code and gives you (best practice) warnings on the fly.
You might also install PSScriptAnalyzer and check your scripts with Invoke-ScriptAnalyzer.
The specific rule that applies to your question can be checked with Get-ScriptAnalyzerRule:
(Get-ScriptAnalyzerRule PSAvoidUsingPositionalParameters).Description
Readability and clarity should be the goal of any script we expect to maintain over time. When calling a command that takes parameters, where possible consider using name parameters as opposed to positional parameters. To fix a violation of this rule, please use named parameters instead of positional parameters when calling a command.
Both ways are valid, the first one is a beat clearer because of the variables names
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
What does this line do in perl?
my %input=CTRL()->{cgi}->Vars;
I don't really understand whats going on here. My best assumption is that Vars is the key and we are inserting those key-value pairs into the input hash.
It's called CTRL. This function returns a reference to a hash. It access the element with keys cgi of this hash. It appears to be an instance of CGI. It calls the Vars method of that object.
Many people want to fetch the entire parameter list as a hash in which the keys are the names of the CGI parameters, and the values are the parameters' values. The Vars() method does this. Called in a scalar context, it returns the parameter list as a tied hash reference. Changing a key changes the value of the parameter in the underlying CGI parameter list. Called in a list context, it returns the parameter list as an ordinary hash. This allows you to read the contents of the parameter list, but not to change it.
When using this, the thing you must watch out for are multivalued CGI parameters. Because a hash cannot distinguish between scalar and list context, multivalued parameters will be returned as a packed string, separated by the "\0" (null) character. You must split this packed string in order to get at the individual values. This is the convention introduced long ago by Steve Brenner in his cgi-lib.pl module for perl version 4, and may be replaced in future versions with array references.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I saw a program in codeforces where it says, "Now Petya wants to compare those two strings lexicographically."
I didn't understand it. What does lexicographically mean?
I'm not sure if this is more of a stackoverflow question or not, but I could be wrong. . . Based on your question and its usage I would prefer a breakdown of the word "lexicographically".
The root appears to be "lexicon", which we can see by this reference the definition is:
"a book containing an alphabetical arrangement of the words in a language and their definitions"
OR
"the vocabulary of a language, an individual speaker or group of speakers, or a subject"
OR (more related to computers)
"the total stock of morphemes in a language"
In this pdf the author says,
"The lexicon of a computer language is its total inventory of words and symbols."
Next, we want to look at the word "lexicography", this is the next layer to our process of building the word up from the root. First, let us look at the general definition. According to Merriam-Webster in this reference we see that lexicography is defined as such:
"the editing or making of a dictionary"
OR
"the principles and practices of dictionary making"
In this reference regarding Computation Lexicography the author states, "Computational Lexicology is the use of computers in the study of the lexicon. It has been more narrowly described by others (Amsler, 1980) as the use of computers in the study of machine-readable dictionaries."
The next-to-last step is to look at the word without its description of the action occurring, that would be "lexicographic", as in lexicographic order. In this reference we see, "In mathematics, the lexicographic or lexicographical order (also known as lexical order, or dictionary order) is a generalization of the alphabetical order of the dictionaries to sequences of ordered symbols or, more generally, of elements of a totally ordered set."
Lastly, we can see that the word is an adverb since in the example sentence it is describing the action that is occurring - also we see the use of "-ly" at the end of the word.
It would appear that Petya would like to compare the two strings alphabetically and with regard to their potentially symbolic, or definitive, meaning within the dictionary of lexicons from which they exist.
A fun little analysis project that took me on.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am confused about where and how often I should declare function return types in common lisp. If I understand correctly, no implementation is required to use the information provided by the declarations, and when the information is used, the effects are not well defined and probably not consistent across different implementations, so this is more of a question of best practices than formal definitions. Please keep that in mind if you try to answer these. Basically, this is what I want to know: Assuming that I'm aiming for maximal efficiency ((optimize (debug 0) (safety 0) speed)), when do function return type declarations provide, in principle, useful information that the compiler can use for optimization? That's a broad question, and I'll take broad answers; but to give a better idea of what I'm after, let me break it down into a couple of specific questions. Given the following definitions:
(defun foo (a)
(the <type> <form>))
(defun bar (a)
(foo a))
(defun baz (a)
(bar a))
a. Can the compiler optimize calls to BAZ, or should the return forms in BAR and BAZ be wrapped in a (seemingly redundant) THE form like it is in FOO? In other words, will the compiler know to treat (bar <form>) as (the <type> (bar <form>)) without me saying so explicitly?
b. Do the relative order of the three definitions affect the answer to (a)?
c. If the above definitions occurred in three separate source files that got compiled into three separate fasl files, how would that change the answer to (a)?
d. Given the following:
(let ((var1 (foo <form1>))
(var2 (bar <form2>))
(var3 (baz <form3>)))
<form>*)
Can/will the compiler correctly infer the types of (the objects bound to) VAR1, VAR2, and VAR3 within the body of the LET without explicit type declarations, or should I add another DECLARE form right after the bindings?
e. Supposing that the LET from question (d) occurs in a file other than the file(s) in which the three functions are defined, what effect will the following declarations:
(declaim (ftype (function (t) <type>) foo bar baz))
at the top of the file have on the answer to (d)?
To answer the question in your title, you should use declarations when you want the human reader to know that certain values have certain types.
Premature optimization is a waste of programmer's time and machine cycles.
Now, your specific list:
a. What kind of optimization do you have in mind? The compiler will know the return value type, but how it will use this knowledge is very much implementation dependent.
b,c. Most likely yes. Otherwise it will have to load the file before compilation or recompile bar after it sees foo.
d. Probably yes.
e. If the compiler knows about the functions foo &c when it is compiling the let form, it should be able to use that knowledge. If the files where the functions are defined have not been loaded yet, then the declaration is necessary. Note however that you might get in a very serious trouble (e.g., segfault) if you lie to the compiler.
Bit of background, I'm a total lisp noob, only started a few weeks ago, but I've been developing in other langs for years. Logic no problem, lisp, problem.
I'm trying to write a macro that will define two clsql classes for me to get around a problem with the library. I'd like the classes to be named x and `x-insert`` , so within the macro I'd like the macro to compute the symbol name of x-insert, but I'm having difficulity doing this. My attempt is below, but i'm stumped on two things.
How do I get it to create the class names. If i remove the space in ,class -insert, it wont eval, which I understand, so I presume I'm missing some straightforward way to tell it to ignore the space,and create the name as a single word, and the second problem is getting it to create two classes, not one, as its only expanding the last part of the macro from what I can see using macro expand.
Perhaps I'm going about this the wrong way altogether, so feel free to kick me in the right direction.
(defmacro gen-pair (class base-slots pkey-slot base-table)
`(clsql:def-view-class ,class -insert()
(
,base-slots
)
(:base-table ,base-table)
)
`(clsql:def-view-class ,class (,class -insert)
(
,pkey-slot
)
(:base-table ,base-table)
)
)
It is difficult to begin an explanation here, since you seem to have a
whole stack of misconceptions.
First question (how to compose symbol names): Lisp macros do not
operate on text but on code. In a backquote form, ,class
evaluates to the code passed into the class parameter of the macro,
most likely a class name in this case. Writing another symbol after
that does not magically merge the symbol names; why should it? If you
want to compose a new symbol name, you have to construct it:
,(intern (string-upcase (concatenate 'string
(symbol-name class)
"-insert")))
Second question (why it seems to expand only the second part): the
contents of a defmacro form are evaluated in an implicit progn
(that is why it does not complain about an invalid number of arguments
here). The return value of the last form is the return value of the
whole defmacro form. In this case, the return value is the code
produced by that backquote form. A macro defines a function that
expands a form into a new form; you cannot expand it into two
unrelated forms. You have to produce a progn form that contains the
two forms you want to have.
Third question (why your code looks so different from what Lispers
write): do not throw around parentheses like nail clippings. There
are several Lisp style guides flying around on the net. Read them.
Wer die Form beherrscht, kann mit ihr spielen (roughly: when you
know the proper way, you can play with it).
Fourth question (how to come around the perceived limitation of
clsql): you could ask that question directly, no? What limitation do
you mean?