|
View:
New views
20 Messages
—
Rating Filter:
Alert me
|
| < Prev | 1 - 2 | Next > |
|
|
Reading, Learning, Confusedby a small bit of example code in Programming Erlang related to guards
and short circuit booleans: f(X) when (X == 0) or (1/X > 2) -> ... g(X) when (X == 0) orelse ( 1/X > 2) -> ... The guard in f(X) fails when X is zero but succeeds in g(X) Can someone explain why? _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedSean Allen wrote:
> by a small bit of example code in Programming Erlang related to guards > and short circuit booleans: > > f(X) when (X == 0) or (1/X > 2) -> > ... > > g(X) when (X == 0) orelse ( 1/X > 2) -> > ... > > The guard in f(X) fails when X is zero but succeeds in g(X) > > Can someone explain why? Sean, The thing is, "or" does not short-circuit evaluation when left side succeeds, whereas "orelse" does. Same short-circuit logic is behind the differences between "and" and "andalso". Actually, the very book you read explains these differences and warns about caveats a couple pages later (or earlier). Don't stop reading. -- vlm _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedOn Jul 19, 2008, at 9:50 AM, Lev Walkin wrote: > Sean Allen wrote: >> by a small bit of example code in Programming Erlang related to >> guards and short circuit booleans: >> f(X) when (X == 0) or (1/X > 2) -> >> ... >> g(X) when (X == 0) orelse ( 1/X > 2) -> >> ... >> The guard in f(X) fails when X is zero but succeeds in g(X) >> Can someone explain why? > > > Sean, > > The thing is, "or" does not short-circuit evaluation when left side > succeeds, whereas "orelse" does. Same short-circuit logic is > behind the differences between "and" and "andalso". > > Actually, the very book you read explains these differences and warns > about caveats a couple pages later (or earlier). Don't stop reading. Actually its about 49 pages later where short circuit booleans are discussed and 37 pages for boolean expressions. *). How is ' f(X) when (X == 0) or (1/X > 2)' and or if it fails for 0? What is the difference at that point between and/or. I cant find anything really detailed on that. Is or equivalent to , and orelse equiv to ;? That is the only way this makes any sense to me. Except that well -- page 94... boolean expressions: 3> true or false true 4> (2 > 1 ) or ( 3 > 4 ) true makes sense. still cant wrap my head around given the above... why.... f(X) when (X == 0) or (1/X > 2) fails. does it fail because it would be a divide by zero? if yes, why does this also fail with divide by zero? (X == 0) orelse (1/X > 2) and why wouldnt the g(X) guard fail? and lord so confused... why are both of these true then: 1> X=1. 1 2> ( X == 1 ) or ( 1/X > 2 ). true 3> ( X == 1 ) orelse ( 1/X > 2 ). true the above... that makes sense to me. those guards... totally lost. TOTALLY. _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedBtw. the Erlang Reference Manual says that
As of Erlang 5.5/OTP R11B, short-circuit boolean expressions are allowed in guards. In guards, however, evaluation is always short-circuited since guard tests are known to be free of side effects. (Section 6.14, Short-Circuit Boolean Expressions) Something is wrong here, isn;t it? Regards, Alpar On Sat, 2008-07-19 at 06:50 -0700, Lev Walkin wrote: > Sean Allen wrote: > > by a small bit of example code in Programming Erlang related to guards > > and short circuit booleans: > > > > f(X) when (X == 0) or (1/X > 2) -> > > ... > > > > g(X) when (X == 0) orelse ( 1/X > 2) -> > > ... > > > > The guard in f(X) fails when X is zero but succeeds in g(X) > > > > Can someone explain why? > > > Sean, > > The thing is, "or" does not short-circuit evaluation when left side > succeeds, whereas "orelse" does. Same short-circuit logic is > behind the differences between "and" and "andalso". > > Actually, the very book you read explains these differences and warns > about caveats a couple pages later (or earlier). Don't stop reading. > _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedYou are getting twisted in knots.
1> X =0. 0 2> (X == 0) orelse (1/X > 2). true 3> f(). ok 4> X = 1. 1 5> (X == 0) orelse (1/X > 2). false 8> f(). 9> X = 0. 0 10> (X == 0) or (1/X > 2). ** exception error: bad argument in an arithmetic expression in operator '/'/2 called as 1 / 0 So the orelse construct works as advertised. It will not evaluate any expressions after the first false condition and prevents divide by zero. -module(guard). -compile([export_all]). test(X) when (X == 0) orelse (1/X > 2) -> true; test(_) -> false. 1> c(guard). {ok,guard} 2> guard:test(0). true 3> guard:test(0.0). true 4> guard:test(0.5). false 5> guard:test(0.4). true Hope this helps. On Sat, Jul 19, 2008 at 10:21 AM, Sean Allen <sean@...> wrote:
-- The great enemy of the truth is very often not the lie -- deliberate, contrived and dishonest, but the myth, persistent, persuasive, and unrealistic. Belief in myths allows the comfort of opinion without the discomfort of thought. John F. Kennedy 35th president of US 1961-1963 (1917 - 1963) _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedI meant
"orelse" will not evaluate any expressions after the first TRUE condition and in this example, prevents divide by zero. You also could have written the test as (X =/= 0) andalso (1/X > 2) On Sat, Jul 19, 2008 at 10:47 AM, Edwin Fine <erlang-questions_efine@...> wrote:
-- The great enemy of the truth is very often not the lie -- deliberate, contrived and dishonest, but the myth, persistent, persuasive, and unrealistic. Belief in myths allows the comfort of opinion without the discomfort of thought. John F. Kennedy 35th president of US 1961-1963 (1917 - 1963) _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedOn Jul 19, 2008, at 10:47 AM, Edwin Fine wrote: > You are getting twisted in knots. > > 1> X =0. > 0 > 2> (X == 0) orelse (1/X > 2). > true > 3> f(). > ok > 4> X = 1. > 1 > 5> (X == 0) orelse (1/X > 2). > false > 8> f(). > 9> X = 0. > 0 > 10> (X == 0) or (1/X > 2). > ** exception error: bad argument in an arithmetic expression > in operator '/'/2 > called as 1 / 0 > > So the orelse construct works as advertised. It will not evaluate > any expressions after the first false condition and prevents divide > by zero. i need to reinstall because orelse doesnt for me. it works the exact same or. but assuming i get a working install the reason in the example that or fails is because of the divide by zero correct? which raises a new question... why does or evaluate all the conditions? what purpose does that serve? when would you use or instead of orelse? _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, Confused-module(guard).
-compile([export_all]). test(X) when X == 0; 1/X > 2 -> true; test(_) -> false. 6> c(guard). {ok,guard} 7> guard:test(0). true 8> guard:test(0.4). true 9> guard:test(0.6). false 10> On Sat, Jul 19, 2008 at 10:31 AM, Alpár Jüttner <alpar@...> wrote: Btw. the Erlang Reference Manual says that -- The great enemy of the truth is very often not the lie -- deliberate, contrived and dishonest, but the myth, persistent, persuasive, and unrealistic. Belief in myths allows the comfort of opinion without the discomfort of thought. John F. Kennedy 35th president of US 1961-1963 (1917 - 1963) _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedI cannot imagine of any situation where or and orelse work the same and reinstalling changes that. Please could you paste your exact code and shell session, showing the problem? Also, which version of Erlang are you using?
On Sat, Jul 19, 2008 at 10:51 AM, Sean Allen <sean@...> wrote:
-- The great enemy of the truth is very often not the lie -- deliberate, contrived and dishonest, but the myth, persistent, persuasive, and unrealistic. Belief in myths allows the comfort of opinion without the discomfort of thought. John F. Kennedy 35th president of US 1961-1963 (1917 - 1963) _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, Confusedwell i am reinstalling now but this was still around in the term...
mac os x if that makes a difference. Eshell V5.5.5 (abort with ^G) 1> X = 0. 0 2> (X == 0 ) or (1/X > 2). =ERROR REPORT==== 19-Jul-2008::10:15:21 === Error in process <0.30.0> with exit value: {badarith,[{erlang,'/', [1,0]},{erl_eval,do_apply,5},{erl_eval,expr,5},{erl_eval,expr,5}, {shell,exprs,6},{shell,eval_loop,3}]} ** exited: {badarith,[{erlang,'/',[1,0]}, {erl_eval,do_apply,5}, {erl_eval,expr,5}, {erl_eval,expr,5}, {shell,exprs,6}, {shell,eval_loop,3}]} ** 3> (X == 0 ) orelse (1/X > 2). =ERROR REPORT==== 19-Jul-2008::10:15:50 === Error in process <0.33.0> with exit value: {badarith,[{erlang,'/', [1,0]},{erl_eval,do_apply,5},{erl_eval,expr,5},{erl_eval,expr,5}, {shell,exprs,6},{shell,eval_loop,3}]} ** exited: {badarith,[{erlang,'/',[1,0]}, {erl_eval,do_apply,5}, {erl_eval,expr,5}, {erl_eval,expr,5}, {shell,exprs,6}, {shell,eval_loop,3}]} ** will be done installing new copy soon... we see what happens then. On Jul 19, 2008, at 10:55 AM, Edwin Fine wrote: > I cannot imagine of any situation where or and orelse work the same > and reinstalling changes that. Please could you paste your exact > code and shell session, showing the problem? Also, which version of > Erlang are you using? > _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedCan you install Erlang V5.6.3 (R12B-3)? You are running 5.5.5.
On Sat, Jul 19, 2008 at 10:59 AM, Sean Allen <sean@...> wrote: well i am reinstalling now but this was still around in the term... mac os x if that makes a difference. -- The great enemy of the truth is very often not the lie -- deliberate, contrived and dishonest, but the myth, persistent, persuasive, and unrealistic. Belief in myths allows the comfort of opinion without the discomfort of thought. John F. Kennedy 35th president of US 1961-1963 (1917 - 1963) _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedOn Jul 19, 2008, at 10:55 AM, Edwin Fine wrote: > I cannot imagine of any situation where or and orelse work the same > and reinstalling changes that. Please could you paste your exact > code and shell session, showing the problem? Also, which version of > Erlang are you using? ok i reinstalled and: Eshell V5.5.5 (abort with ^G) 1> X = 0. 0 2> (X == 0 ) or (1/X > 2). =ERROR REPORT==== 19-Jul-2008::11:02:12 === Error in process <0.30.0> with exit value: {badarith,[{erlang,'/', [1,0]},{erl_eval,do_apply,5},{erl_eval,expr,5},{erl_eval,expr,5}, {shell,exprs,6},{shell,eval_loop,3}]} ** exited: {badarith,[{erlang,'/',[1,0]}, {erl_eval,do_apply,5}, {erl_eval,expr,5}, {erl_eval,expr,5}, {shell,exprs,6}, {shell,eval_loop,3}]} ** 3> (X == 0 ) orelse (1/X > 2). true which makes a lot more sense. i have no idea why or and orelse where the same before. which leaves the question ( after answering a lot of confusion )... why would you want to use or instead of orelse ? _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedSean,
I think it's historical. "orelse" and "andalso" were added to the language later. I would also be inclined to use the short-circuit ones, but I don't find myself writing a lot of code that uses "and" and "or" anyway. I might have written your example as f(X) when X == 0; 1/X < 2 -> which gets evaluated from left to right. On Sat, Jul 19, 2008 at 11:05 AM, Sean Allen <sean@...> wrote:
-- The great enemy of the truth is very often not the lie -- deliberate, contrived and dishonest, but the myth, persistent, persuasive, and unrealistic. Belief in myths allows the comfort of opinion without the discomfort of thought. John F. Kennedy 35th president of US 1961-1963 (1917 - 1963) _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedAlpár Jüttner wrote:
> Btw. the Erlang Reference Manual says that > > As of Erlang 5.5/OTP R11B, short-circuit boolean expressions are > allowed in guards. In guards, however, evaluation is always > short-circuited since guard tests are known to be free of side > effects. > (Section 6.14, Short-Circuit Boolean Expressions) > > Something is wrong here, isn;t it? Not really, but it's a bit cryptic. The text only talks about evaluation of guard tests, i.e., the complete expressions separated by commas and/or semicolons. The commas/semicolons are always short-circuited. But they cannot be nested arbitrarily (you can have semicolon-separated groups of comma-separated expressions, but not the other way around and not grouped using parentheses). To write more complex expressions and still have short-circuiting, you need andalso/orelse. The strictness of the and/or operators is historical; I think the reasoning was that evaluating both sides gave a more predictable behaviour, with regard to possible exceptions and evaluation time. This was probably a mistake, but it was too late to fix, so andalso/orelse were added. The short answer to "when would anyone want to use plain and/or?" is "rarely". A slightly longer answer would be "when readability and horizontal space is more important than shaving off a few cycles". /Richard _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, Confused> As of Erlang 5.5/OTP R11B, short-circuit boolean expressions are
> allowed in guards. In guards, however, evaluation is always > short-circuited since guard tests are known to be free of side > effects. > (Section 6.14, Short-Circuit Boolean Expressions) > > Something is wrong here, isn;t it? I mean * What does the word "however" mean here? Does it mean that if they are not in a guard, orelse/andelse might be non short-circuited? * How does the freedom from side effects are related to the short-circuited evaluation? Regards, Alpar > > Regards, > Alpar > > On Sat, 2008-07-19 at 06:50 -0700, Lev Walkin wrote: > > Sean Allen wrote: > > > by a small bit of example code in Programming Erlang related to guards > > > and short circuit booleans: > > > > > > f(X) when (X == 0) or (1/X > 2) -> > > > ... > > > > > > g(X) when (X == 0) orelse ( 1/X > 2) -> > > > ... > > > > > > The guard in f(X) fails when X is zero but succeeds in g(X) > > > > > > Can someone explain why? > > > > > > Sean, > > > > The thing is, "or" does not short-circuit evaluation when left side > > succeeds, whereas "orelse" does. Same short-circuit logic is > > behind the differences between "and" and "andalso". > > > > Actually, the very book you read explains these differences and warns > > about caveats a couple pages later (or earlier). Don't stop reading. > > > > _______________________________________________ > erlang-questions mailing list > erlang-questions@... > http://www.erlang.org/mailman/listinfo/erlang-questions _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
|
|
Re: Reading, Learning, ConfusedOn Jul 19, 2008, at 11:10 AM, Edwin Fine wrote: > Sean, > > I think it's historical. "orelse" and "andalso" were added to the > language later. I would also be inclined to use the short-circuit > ones, but I don't find myself writing a lot of code that uses "and" > and "or" anyway. I might have written your example as > > f(X) when X == 0; 1/X < 2 -> > > which gets evaluated from left to right. that makes sense thanks. so, something got fubar in the version i had installed.... and suddenly things make much more sense. i' |