side-effects

Are side-effects possible in pure functional programming

爱⌒轻易说出口 提交于 2019-11-27 12:22:31
问题 I have been trying to wrap my head around functional programming for a while now? I have looked up lambda calculus, LISP, OCML, F# and even combinatorial logic but the main problem I have is how do you do things that require side effects like (interacting with a user, communicating with a remote service, or even handle simulating using random sampling) without violating the fundamental premise of pure functional programming which is, that for a given input the output is deterministic? I hope

Is the use of del bad?

♀尐吖头ヾ 提交于 2019-11-27 11:28:14
问题 I commonly use del in my code to delete objects: >>> array = [4, 6, 7, 'hello', 8] >>> del(array[array.index('hello')]) >>> array [4, 6, 7, 8] >>> But I have heard many people say that the use of del is unpythonic. Is using del bad practice? >>> array = [4, 6, 7, 'hello', 8] >>> array[array.index('hello'):array.index('hello')+1] = '' >>> array [4, 6, 7, 8] >>> If not, why are there many ways to accomplish the same thing in python? Is one better than the others? Option 1: using del >>> arr =

What are expressions with side effects and why should they be not passed to a macro?

有些话、适合烂在心里 提交于 2019-11-27 09:29:27
I came across a statement in the text C How to Program: "Expressions with side effects (i.e., variable values are modified) should not be passed to a macro because macro arguments may be evaluated more than once.". My question is what are expressions with side effects and why should they be not passed to a macro? Some programmer dude The classic example is a macro to calculate the maximum of two value: #define MAX(a, b) ((a) > (b) ? (a) : (b)) Now lets "call" the macro like this: int x = 5; int y = 7; int z = MAX(x++, y++); Now if MAX was a normal function, we would expect that x and y would

Are side effects a good thing?

一曲冷凌霜 提交于 2019-11-27 01:03:59
问题 I feel the term rather pejorative. Hence, I am flabbergasted by the two sentences in Wikipedia: Imperative programming is known for employing side effects to make programs function. Functional programming in turn is known for its minimization of side effects. [1] Since I am somewhat Math-biased, the latter sounds excellent. What are the arguments for side-effects? Do they mean the loss of control or the acceptance of uncertainty? Are they a good thing? 回答1: Every so often I see a question on

Purity vs Referential transparency

半世苍凉 提交于 2019-11-27 00:25:39
问题 The terms do appear to be defined differently, but I've always thought of one implying the other; I can't think of any case when an expression is referentially transparent but not pure, or vice-versa. Wikipedia maintains separate articles for these concepts and says: From Referential transparency: If all functions involved in the expression are pure functions, then the expression is referentially transparent. Also, some impure functions can be included in the expression if their values are

What exactly does “effectful” mean

爷,独闯天下 提交于 2019-11-26 19:25:47
问题 Time and again I read the term effectful , but I am still unable to give a clear definition of what it means. I assume the correct context is effectful computations , but I've also seen the term effectful values ) I used to think that effectful means having side effects . But in Haskell there are no side-effects (except to some extent IO). Still there are effectful computations all over the place. Then I read that monads are used to create effectful computations. I can somewhat understand

Unsequenced value computations (a.k.a sequence points)

喜你入骨 提交于 2019-11-26 18:58:44
Sorry for opening this topic again, but thinking about this topic itself has started giving me an Undefined Behavior. Want to move into the zone of well-defined behavior. Given int i = 0; int v[10]; i = ++i; //Expr1 i = i++; //Expr2 ++ ++i; //Expr3 i = v[i++]; //Expr4 I think of the above expressions (in that order) as operator=(i, operator++(i)) ; //Expr1 equivalent operator=(i, operator++(i, 0)) ; //Expr2 equivalent operator++(operator++(i)) ; //Expr3 equivalent operator=(i, operator[](operator++(i, 0)); //Expr4 equivalent Now coming to behaviors here are the important quotes from C++ 0x .

Why can I call a non-constexpr function inside a constexpr function?

一个人想着一个人 提交于 2019-11-26 16:35:08
问题 Consider the following code: #include <stdio.h> constexpr int f() { return printf("a side effect!\n"); } int main() { char a[f()]; printf("%zd\n", sizeof a); } I would have expected the compiler to complain about the call to printf inside f , because f is supposed to be constexpr , but printf is not. Why does the program compile and print 15? 回答1: The program is ill-formed and requires no diagnostic according to the C++11 draft standard section 7.1.5 The constexpr specifier paragraph 5 which

What are expressions with side effects and why should they be not passed to a macro?

霸气de小男生 提交于 2019-11-26 14:45:58
问题 I came across a statement in the text C How to Program: "Expressions with side effects (i.e., variable values are modified) should not be passed to a macro because macro arguments may be evaluated more than once.". My question is what are expressions with side effects and why should they be not passed to a macro? 回答1: The classic example is a macro to calculate the maximum of two value: #define MAX(a, b) ((a) > (b) ? (a) : (b)) Now lets "call" the macro like this: int x = 5; int y = 7; int z

Unsequenced value computations (a.k.a sequence points)

主宰稳场 提交于 2019-11-26 06:44:06
问题 Sorry for opening this topic again, but thinking about this topic itself has started giving me an Undefined Behavior. Want to move into the zone of well-defined behavior. Given int i = 0; int v[10]; i = ++i; //Expr1 i = i++; //Expr2 ++ ++i; //Expr3 i = v[i++]; //Expr4 I think of the above expressions (in that order) as operator=(i, operator++(i)) ; //Expr1 equivalent operator=(i, operator++(i, 0)) ; //Expr2 equivalent operator++(operator++(i)) ; //Expr3 equivalent operator=(i, operator[]