记录类型的默认相等性?
Default equality for record types?
我从 The Purescript Book 的第 3 章结尾开始做这个练习:
Write a function which looks up an Entry given a street address, by reusing the existing code in findEntry. Test your function in PSCi.
我试过了:
findByAddress address = head <<< filter filterEntry
where
filterEntry :: Entry -> Boolean
filterEntry entry = entry.address == address
哪些错误:
No type class instance was found for
Data.Eq.Eq { street :: String
, city :: String
, state :: String
}
while applying a function eq
of type Eq t0 => t0 -> t0 -> Boolean
to argument entry.address
while inferring the type of eq (entry.address)
in value declaration findByAddress
问题1:我认为记录类型有一个默认的相等函数,即所有的记录属性都是相等的。有吗?如何使用?
问题 2: 为了防止我在问题 1 中的上述假设是错误的,我尝试了:
eq :: Address -> Address -> Boolean
eq address1 address2 = address1.street == address2.street &&
address1.city == address2.city &&
address1.state == address2.state
但这并没有解决问题。为什么不呢?
bower install purescript-record-extra
pulp repl
> import Data.Record.Extra (eqRecord)
> {a:1, b:2.5} `eqRecord` {a:1, b:2.5}
true
> {a:1, b:2.5} `eqRecord` {a:1, b:3.5}
false
-- different record types cannot be compared
> {a:1, b:2.5} `eqRecord` {a:1, b:3.5, c:"abc"}
Error found:
in module $PSCI
at line 1, column 25 - line 1, column 45
Type of expression contains additional label c.
while checking that expression { a: 1
, b: 3.5
, c: "abc"
}
has type { a :: Int
, b :: Number
}
while applying a function ((eqRecord (#dict RowToList t2 t1)) (#dict EqRecord t1 t2)) { a: 1
, b: 2.5
}
of type { | t0 } -> Boolean
to argument { a: 1
, b: 3.5
, c: "abc"
}
in value declaration it
where t0 is an unknown type
更新 pkg purescript-record 具有相等操作。
> import Data.Record (equal)
> {a:1, b:2.5} `equal` {a:1, b:2.5}
true
> {a:1, b:2.5} `equal` {a:1, b:3.5}
false
> {a:1, b:2.5} `equal` {a:1, b:2.5, c:"abc"}
Error found:
in module $PSCI
at line 1, column 22 - line 1, column 42
Type of expression contains additional label c.
while checking that expression { a: 1
, b: 2.5
, c: "abc"
}
has type { a :: Int
, b :: Number
}
使用新类型和派生实例:
> newtype Address = Address {street::String, city::String}
> derive instance eqAddress :: Eq Address
> Address {street:"la Rambla", city:"Barcelona"} == Address {street:"main", city:"LA"}
false
我从 The Purescript Book 的第 3 章结尾开始做这个练习:
Write a function which looks up an Entry given a street address, by reusing the existing code in findEntry. Test your function in PSCi.
我试过了:
findByAddress address = head <<< filter filterEntry
where
filterEntry :: Entry -> Boolean
filterEntry entry = entry.address == address
哪些错误:
No type class instance was found for
Data.Eq.Eq { street :: String
, city :: String
, state :: String
}
while applying a function eq
of type Eq t0 => t0 -> t0 -> Boolean
to argument entry.address
while inferring the type of eq (entry.address)
in value declaration findByAddress
问题1:我认为记录类型有一个默认的相等函数,即所有的记录属性都是相等的。有吗?如何使用?
问题 2: 为了防止我在问题 1 中的上述假设是错误的,我尝试了:
eq :: Address -> Address -> Boolean
eq address1 address2 = address1.street == address2.street &&
address1.city == address2.city &&
address1.state == address2.state
但这并没有解决问题。为什么不呢?
bower install purescript-record-extra
pulp repl
> import Data.Record.Extra (eqRecord)
> {a:1, b:2.5} `eqRecord` {a:1, b:2.5}
true
> {a:1, b:2.5} `eqRecord` {a:1, b:3.5}
false
-- different record types cannot be compared
> {a:1, b:2.5} `eqRecord` {a:1, b:3.5, c:"abc"}
Error found:
in module $PSCI
at line 1, column 25 - line 1, column 45
Type of expression contains additional label c.
while checking that expression { a: 1
, b: 3.5
, c: "abc"
}
has type { a :: Int
, b :: Number
}
while applying a function ((eqRecord (#dict RowToList t2 t1)) (#dict EqRecord t1 t2)) { a: 1
, b: 2.5
}
of type { | t0 } -> Boolean
to argument { a: 1
, b: 3.5
, c: "abc"
}
in value declaration it
where t0 is an unknown type
更新 pkg purescript-record 具有相等操作。
> import Data.Record (equal)
> {a:1, b:2.5} `equal` {a:1, b:2.5}
true
> {a:1, b:2.5} `equal` {a:1, b:3.5}
false
> {a:1, b:2.5} `equal` {a:1, b:2.5, c:"abc"}
Error found:
in module $PSCI
at line 1, column 22 - line 1, column 42
Type of expression contains additional label c.
while checking that expression { a: 1
, b: 2.5
, c: "abc"
}
has type { a :: Int
, b :: Number
}
使用新类型和派生实例:
> newtype Address = Address {street::String, city::String}
> derive instance eqAddress :: Eq Address
> Address {street:"la Rambla", city:"Barcelona"} == Address {street:"main", city:"LA"}
false