我对 Haskell 编程很陌生。我正在尝试处理它的类、数据、实例和新类型。这是我的理解:
data NewData = Constr1 Int Int | Constr2 String Float
与(Java 或 C#)大致相同:
class NewData {
private int a, b;
private string c;
private float d;
/* get'ers and set'ers for a, b, c and d
................
*/
private NewData() { }
private NewData(int a, int b) {
this.a = a;
this.b = b;
}
private NewData(string c, float d) {
this.c = c;
this.d = d;
}
public static Constr1(int a, int b) {
return new NewData(a, b);
}
public static Constr2(string c, float d) {
return new NewData(c, d);
}
}
和
class SomeClass a where
method1 :: [a] -> Bool
为了
interface SomeInterface<T> {
public bool method1(List<T> someParam);
}
// or
abstract class SomeClass<T> {
public abstract bool method1(List<T> someParam);
}
和
instance SomeClass Int where
method1 a = 5 == head a -- this doesn't have any meaning, though, but this is correct
为了
class SomeClassInstance<Int>: SomeClass {
public bool method1(List<Int> param) {
return param.first == 5; // I don't remember the method's name exactly, it doesn't matter
}
}
这些都是正确的吗?newtype 怎么样,我如何用 C# 或 Java 来表示它?