The meaning of side-effect in Clojure [closed] - clojure

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 7 years ago.
Improve this question
I was thinking on the meaning of side-effect in Clojure. What exactly is a side-effect in Clojure? Could any one explain this with an example?

A side effect in any programming language would be everything that is done which does not have a direct correlation between supplied arguments and the returned result.
(+ 3 4) ; ==> 7 (result is always a mapping between arguments and result. It will always be 7 no matte rhow many times you do it.
(rand-int 4) ; ==> 0,1,2, or 3. You have no idea what it will produce next.
The first expression is functional. You can make a lookup table of all the different two values with it's result and you wouldn't know the difference.
The second might give different result for the same argument. The computation must be based on something else, like internal state, and not the argument alone. It has side effects.
Typical side effects used in programs are I/O and object mutation.

Related

postincrement operation in same statement [closed]

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 5 years ago.
Improve this question
the question is to understand how the standards define or allow to handle these situations and what would be behaviour in this particular case wherein the variable undergoing post/pre increment is used in same statement as that of expression, when it is being used as argument to function call.
take for example following sample code
char a[SZ];
which of the following would be correct?
strlcpy(&a[i++],"Text",SZ-i-1);
strlcpy(&a[i++],"Text",SZ-i);
if the
"," comma
would used for computation of i++ or
";" semicolon
??
In this case, since the "comma separated expressions" are parameters of a function (strlcpy), the order of evaluation of the expressions is unspecified, even in C++17.
However, C++17 guarantees that expression evaluation won't be interleaved between arguments, so that each expression is fully formed before forming another one.
So, in your strlcpy(&a[i++],"Text",SZ-i), you cannot rely on the value of i: it could exhibit a different behavior depending on your implementation. Though since it's not undefined behavior, you know it's either going to be the old value of i, or the old value plus one.

is there any plausible scenario in which a programmer might wish to avoid shortcircuit evaluation of a Boolean expression? [closed]

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 6 years ago.
Improve this question
short circuit evaluation can shorten the compile time, so i learned that C, C++ is using that way. But are there any situations that short-circuit evaluation ruining the code?
Short circuiting does not shorten the compile time of the code. (by any meaningful amounts, at least)
It might be shortening the runtime, but it's not its intended purpose.
The purpose of short circuiting is to do the minimal amount of work in order to check a certain condition.
For example:
When using && (as opposed to a single &), the right operand won't be evaluated if the left one is false. This is due to the nature of a logical and operation: if at least one of the operands is false, the whole expression is false.
Technically, it will shorten the runtime if the condition fails early, but the amount of saved runtime is dependent on the expressions inside each operand.
Anyway, it's incorrect to use && because it's "faster" than &. You should use either when appropriate.
& is used for bitwise operations.

How to implement an interface via protocols? [closed]

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 7 years ago.
Improve this question
Using extend-protocol, a protocol P can provide a default implementation for anything that implements interface I. This essentially teaches I's to do new things. If we want a type or record to provide the functionality of I we still need to extend them with I.
Is there a way to specify I behavior (it's methods) in terms of P's behavior?
What am I actually trying to accomplish
My protocol P (which is like a stream) has (seq [this] [this timeout-value]) to provide sequence access. The second arity is to return a special value if the stream expired. (Ending the sequence there would be confusing.)
P also has (close [this]).
I would like objects that extend P to be usable in clojure.core/seq (being a Sequable) and also implement java.io.Closeable. One way to accomplish this is to remove those methods from P and just implement Sequable & Closeable within the type/record. But then when somebody hands me a P can't be sure if it can be closed or seqed. Call me object oriented, but P extends I.
Looking for
It's not possible (for now).
It can be done with this code ...
It can be redesigned to achieve a similar effect ...
If P's seq being multiple arity is an issue, seq & seq' would do as well.

Declaring certain elements as const in an array c++, sudoku solver [closed]

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 8 years ago.
Improve this question
Is it possible for me to take an existing array and make certain values (not all) constant? I'm trying to build a sudoku solver and my idea is to have the user enter values, so I would like to have those values remain constant as I change the empty spaces. Any tips or advice with the solver would also be appreciated. This is my first quarter working with c++. Thanks!
No. Const is essentially a compile-time idea. It's not something that can be toggled as a program is running.
If you need certain values to remain untouched while others are changed, then you need to put that into your data and logic. For instance, each value might have an associated boolean that indicates whether or not it can be changed. Then write your logic to respect that boolean.

NFA to Regular Expression [closed]

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 8 years ago.
Improve this question
I have a picture of an NFA, and need to get the resulting regular expression from the NFA. I first had aba*, but realized this doesn't work. I believe it must start with (a|b), but am not sure where to go from there.
There are general approaches on how to translate an NFA to a regular expression, but I do not remember how. However in your case we are helped by the fact that all loops of length more than 1 starts in state 0. So we can use the following regexp;
(b?ab|aab|ab+aa?b)*(ba?|aa|ab+aa?)
Explanation;
(b?ab|aab|ab+aa?b)* is what the regexp would be if state 0 was the only matching state. Since all long loops goes through state 0 this is a prefix of our regexp.
(ba?|aa|ab+aa?) is what our regexp would be if the b links going back to state 0(from state 1 and state 4) where not present. Because we are in state 0 after the previous loop this takes us to an accepting state.
Bringing the two together we have a regexp that looks for traces that goes returns to state 0 an arbitrary number of times and then move from state 0 to an accepting state.
Starting with (a|b) is the wrong approach, because the states 1 and 3 have little in common and how you are allowed to proceed from an a is different from how you are allowed to proceed from a b.