equality

Pattern matching equality on tuples in Haskell

ⅰ亾dé卋堺 提交于 2019-12-02 10:40:17
问题 For this function on symmetric equality over tuples, symEq :: Eq a => (a,a) -> (a,a) -> Bool symEq (x,y) (u,v) = (x,y) == (u,v) || (x,y) == (v,u) would like to rewrite it using pattern matching as follows, symEq' :: Eq a => (a,a) -> (a,a) -> Bool symEq' (x,y) (x,y) = True symEq' (x,y) (y,x) = True symEq' _ _ = False The latter fails with error on conflicting definitions for x and y . How to rewrite symEq and take benefit of pattern matching ? 回答1: Unlike in some languages (I hear Erlang works

Why does an assignment in an if statement equate to true?

不问归期 提交于 2019-12-02 10:18:36
Let me start off by saying I understand the difference between = , == , and === . The first is used to assign the right-hand value to the left-hand variable, the second is used to compare the equivalency of the two values, and the third is used not just for equivalency but type comparison as well (ie true === 1 would return false ). So I know that almost any time you see if (... = ...) , there's a pretty good chance the author meant to use == . That said, I don't entirely understand what's happening with these scripts: var a = 5; if (a = 6) console.log("doop"); if (true == 2) console.log('doop

How can we check reference equality for a type that implements equality operator?

廉价感情. 提交于 2019-12-02 09:54:04
In C#, how can we check reference equality for a type that implements equality operator? class C { public int Val{get;set;} public static bool operator ==(C c1, C c2) { return c1.Val == c2.Val; } public static bool operator !=(C c1, C c2) { return c1.Val != c2.Val; } } class Program { public static void Main(string[] args) { C c1=new C(){Val=1}; C c2=new C(){Val=1}; Console.WriteLine(c1==c2);//True. but they are not same objects. //How can I Check that? Console.Write("Press any key to continue . . . "); } } If you mean equality by reference, you may use the Object.ReferenceEquals static method

Javascript equality operators

拟墨画扇 提交于 2019-12-02 06:55:37
In David Flanagan's Javascript guide, there is a statement: the == operator never attempts to convert its operands to boolean So here I did a little test: var a = false; var b = ""; // empty string a == b; //returns true Looking at Abstract Equality Comparison Algorithm there is a point: e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false. How can x and y be both true if y is string data type (without conversion)? What happens under the hood is If Type(x) is Boolean , return the result of the comparison ToNumber(x) == y . Number(false) == ""

Return equality from Mathematica function

夙愿已清 提交于 2019-12-02 05:56:42
I have a function that returns equalities, which I want to print, for example, x==y, or 2x+5==10. These usually have no meaning for mathematica, it cannot simplify it furhter. However, sometimes the both sides are equal, but I want to be able to print the equality in unevaluated form: that is, I want Mathematica to print x==x, and not True. A very simple example: Print[printableEqual[x,y]] should print x==y, while Print[printableEqual[x,x]] should print x==x Edit: The reason is that I have a relation among graphs. I would like to return things like G1 == t*G2 + s*G3 where t,s are integers, and

C# object comparison

空扰寡人 提交于 2019-12-02 03:20:36
问题 Could someone point out the idea of overloading operator== to perform deep object comparison (instead of reference comparison). From MSDN: By default, the operator == tests for reference equality by determining whether two references indicate the same object. Therefore, reference types do not have to implement operator == in order to gain this functionality. When a type is immutable, that is, the data that is contained in the instance cannot be changed, overloading operator == to compare

How to distinguish MethodBase in generics

旧城冷巷雨未停 提交于 2019-12-02 01:34:47
I have a cache based on Dictionary<MethodBase, string> The key is rendered from MethodBase.GetCurrentMethod. Everything worked fine until methods were explicitly declared. But one day it is appeared that: Method1<T>(string value) Makes same entry in Dictionary when T gets absolutely different types. So my question is about better way to cache value for generic methods. (Of course I can provide wrapper that provides GetCache and equality encountered generic types, but this way doesn't look elegant). Update Here what I exactly want: static Dictionary<MethodBase, string> cache = new Dictionary

C# object comparison

天涯浪子 提交于 2019-12-02 01:14:32
Could someone point out the idea of overloading operator== to perform deep object comparison (instead of reference comparison). From MSDN: By default, the operator == tests for reference equality by determining whether two references indicate the same object. Therefore, reference types do not have to implement operator == in order to gain this functionality. When a type is immutable, that is, the data that is contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference equality can be useful because, as immutable objects, they can be

javascript truthy numbers

血红的双手。 提交于 2019-12-02 00:17:12
Based on these rules: Falsy: false 0 (zero) '' or "" (empty string) null undefinded NaN (e.g. the result of 1/0) Truthy: Everything else I fail to find the correct explanation as to why in following tests, only number 1 evaluates to "true" 0 == true ("false") 1 == true ("true") 2 == true ("false") othernumber == true ("false") The "truthy" and "falsy" rules only apply when the value itself is being used as the test, e.g.: var str = ""; if (str) { // It's truthy } else { // It's falsy } == has its own, different, set of rules for determining the loose equality of its operands, which are

Why does +[UIColor whiteColor] not equal another white?

ぃ、小莉子 提交于 2019-12-01 22:29:34
问题 This is something really strange. Comparing +[UIColor redColor] with a red I create myself gives an equal result, but comparing +[UIColor whiteColor] to another white does not. // This test passes. XCTAssertEqualObjects([UIColor redColor], [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0], @"Red should equal red."); // While this test fails! XCTAssertEqualObjects([UIColor whiteColor], [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], @"White should equal white."); While I'm