【问题标题】:Non-exhaustive pattern in function in HaskellHaskell 中函数的非穷举模式
【发布时间】:2017-09-15 18:41:50
【问题描述】:

我有一个 Evol 类,并希望将 distanceMatrix 的实例应用于我的 MolSeq 类型的列表。函数 molseqDistMat 可以按需要工作,但我无法理解尝试运行 distanceMatrix [Molseq] 时遇到的错误。我了解错误的含义,但找不到异常。这是错误。

*F2> distanceMatrix l
*** Exception: lab2.hs:79:3-43: Non-exhaustive patterns in function 
distanceMatrix

这是代码。

class Evol a where
  distanceMatrix :: [a] -> [(String, String, Double)]

instance Evol MolSeq where
  distanceMatrix [a] = molseqDistMat [a] [] -- <- Line 79

molseqDistMat :: [MolSeq] -> [(String, String, Double)] -> [(String, 
String, Double)]
molseqDistMat todo res
  | null (tail todo) = res
  | otherwise = molseqDistMat (tail todo) (res++(doRow (head todo) (tail 
todo) []))

doRow :: MolSeq -> [MolSeq] -> [(String, String, Double)] -> [(String, 
String, Double)]
doRow mol rest result
  | null rest = reverse result
  | otherwise = doRow mol (tail rest) ((name mol, name (head rest), 
distance mol (head rest)):result)

【问题讨论】:

  • 使用-Wall 启用警告:您会在编译时发现这一点,GHC 甚至会提供您的定义未涵盖的列表示例。
  • 感谢您的提示!

标签: haskell non-exhaustive-patterns


【解决方案1】:

你可能想要:

distanceMatrix xs = molseqDistMat xs [] -- <- Line 79

[a] 是一种模式,它与一个元素的列表完全匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多