簡體   English   中英

在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覆蓋Either sides 大約2*2例,均勻嗎?

我正在考慮在pattern-matching每個分支上使用fromRight (例如fromRight (D 0) )(在我們的情況下,因為只有2種情況),但是我不知道這是否是最佳方法。 但是fromRight返回內部類型。我想保留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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM