解决 ADT 上的 readEither
Resolving readEither on ADT
您好,我想知道如果给定 Algebraic Data Type
我该如何解决以下问题:
u::Text->String
u =Data.Text.unpack
data Numeric=I Int | D Double
readNumeric::Text->Either String Numeric
readNumeric text=let str=u text in
if '.' `elem` str then
D (readEither str::Either String Double)
else
I (readEither str::Either String Int)
如何在给定 ADT
的情况下覆盖 sides
和 Either
?实际上 2*2
cases , 统一?
我正在考虑在 pattern-matching
的每个分支(例如 fromRight (D 0)
)上使用 fromRight
(在我们的案例中,如果因为只有 2 个案例)但我不知道它是否是
最好的方法。
但是 fromRight
returns 内部类型..我想保留 Either
有什么想法吗?
这不就是你需要的吗?
readNumeric::Text -> Either String Numeric
readNumeric text=let str = u text in
if '.' `elem` str then
fmap D (readEither str::Either String Double)
else
fmap I (readEither str::Either String Int)
您好,我想知道如果给定 Algebraic Data Type
我该如何解决以下问题:
u::Text->String
u =Data.Text.unpack
data Numeric=I Int | D Double
readNumeric::Text->Either String Numeric
readNumeric text=let str=u text in
if '.' `elem` str then
D (readEither str::Either String Double)
else
I (readEither str::Either String Int)
如何在给定 ADT
的情况下覆盖 sides
和 Either
?实际上 2*2
cases , 统一?
我正在考虑在 pattern-matching
的每个分支(例如 fromRight (D 0)
)上使用 fromRight
(在我们的案例中,如果因为只有 2 个案例)但我不知道它是否是
最好的方法。
但是 fromRight
returns 内部类型..我想保留 Either
有什么想法吗?
这不就是你需要的吗?
readNumeric::Text -> Either String Numeric
readNumeric text=let str = u text in
if '.' `elem` str then
fmap D (readEither str::Either String Double)
else
fmap I (readEither str::Either String Int)