编写适当的自定义读取实例

时间:2017-04-23 11:34:17

标签: haskell typeclass currying deriving

你好Haskellers先生,

我现在正在学习Haskell一个月,我正在努力为个人数据类型创建自定义读取实例。

我关注thisrelevant chapter in Learn Yourself a Haskell,这是我的代码段。

data Position      =  Position (Absc,Ordn) deriving (Show)
instance Read (Position) where
readsPrec _ input =
    let absc = List.filter (/='(') $ takeWhile (/=',')
        ordn = List.filter (/=')') $ tail (dropWhile (/=',') )
    in (\str -> Position ( (read (absc str) :: Int)
                       , (read (ordn str) :: Int) ) ) input
type Absc = Int
type Ordn = Int

我的目标是解析输入"(1,3)"以输出类似Position (1,3)

的内容 但是,我收到以下错误消息:

• Couldn't match expected type ‘[Char]’
              with actual type ‘[Char] -> [Char]’
• Probable cause: ‘takeWhile’ is applied to too few arguments
  In the second argument of ‘($)’, namely ‘takeWhile (/= ',')’
  In the expression: filter (/= '(') $ takeWhile (/= ',')
  In an equation for ‘absc’:
      absc = filter (/= '(') $ takeWhile (/= ',')

与ordn函数相同。

• Couldn't match expected type ‘[(Position, String)]’
              with actual type ‘Position’
• In the expression:
    (\ str
       -> Position ((read (absc str) :: Int), (read (ordn str) :: Int)))
      input
  In the expression:
    let
      absc = filter (/= '(') $ takeWhile (/= ',')
      ordn = filter (/= ')') $ tail (dropWhile (/= ','))
    in
      (\ str
         -> Position ((read (absc str) :: Int), (read (ordn str) :: Int)))
        input
  In an equation for ‘readsPrec’:
      readsPrec _ input
        = let
            absc = filter (/= '(') $ takeWhile (/= ',')
            ordn = filter (/= ')') $ tail (dropWhile (/= ','))
          in
            (\ str
               -> Position ((read (absc str) :: Int), (read (ordn str) :: Int)))
              input

我的let语句似乎无法将abscordn识别为函数(或者至少尝试直接应用它们,而我只想将它们定义为部分应用的函数稍后在参数str处应用它们。我也可能搞砸了我的Position值构造函数。

我不熟悉Haskell编码风格,我可能使用了一些我不太了解的关键字和工具。你能否告诉我如何写这个以使其有效?

提前谢谢。

2 个答案:

答案 0 :(得分:3)

您的代码中存在大量错误 - 我认为您应该仔细阅读$.之间的区别,了解处理函数时必不可少的事情,大多数情况下错误是由于尝试将函数应用于其参数($)而不是连接函数(.)

这就是我修改你的代码至少进行类型检查 - 尽管我认为readsPrec不应该保留原始输入,而是在解析完成后保留剩余的字符串。

修复代码

instance Read Position where
  readsPrec _ input =
      let absc = filter (/='(') . takeWhile (/=',')
          ordn = filter (/=')') . tail . dropWhile (/=',')
       in [(Position ( read (absc input) :: Int
                     , read (ordn input) :: Int), input)]

但看到这让我感到不快 - 并不是说​​哈斯克尔说是抽象的,光滑的,而且非常富有表现力。让我们再试一次

整理

instance Read Position where
  readsPrec _ str = let ('(':absc,',':ordn') = break (==',') str
                        (ordn,')':str') = break (==')') ordn'
                     in [(Position (read absc, read ordn), str')]

更好的是,我们捕获到开头和结尾都应该有parens,但仍然有点麻烦。

使用现有功能

知道Tuple已经是Read

的实例
instance Read Position where
  readsPrec s str = [(Position x, rest) | (x,rest) <- readsPrec s str]

很好,但我们仍然可以做得更好

假设我们最近使用元组工作了一下,发现了一个非常方便的模块Data.Bifunctor,它具有函数firstsecond来转换2元组的第一个和第二个组件(任何组件)实际上是bifunctor。)

我们可以将上述内容简化为

instance Read Position where
  readsPrec s = map (first Position) . readsPrec s

干净又简短。

答案 1 :(得分:0)

使用show "(3,4)"::Position返回Position (3,4)。 非常感谢您的详细解答。我倾向于混淆$.符号,但在我完成文档后,它变得清晰了。