相关疑难解决方法(0)

功能上的非详尽模式

我有这个代码的问题,它应该计算字符串中相同字母的最长子字符串,但是有一个错误:

*** Exception: test.hs:(15,0)-(21,17): 
Non-exhaustive patterns in function countLongest'
Run Code Online (Sandbox Code Playgroud)

我知道这是错误的类型问题,但我不知道错误在哪里,或者如何查找或调试它

countLongest :: (Eq a) => [a] -> Int
countLongest' :: (Eq a) => Int -> Int -> [a] -> Int

countLongest a = countLongest' 0 0 a
countLongest' n max (y:x:ys)
        | y == x = countLongest' (n+1) max (x:ys)
        | n > max = countLongest' 0 (n) (x:ys)
        | otherwise = countLongest' 0 (max) (x:ys)
countLongest' n max []
        | n > max = n
        | otherwise = max
Run Code Online (Sandbox Code Playgroud)

haskell pattern-matching non-exhaustive-patterns

23
推荐指数
1
解决办法
4万
查看次数

在案件中非穷举模式的更好例外

有没有办法让GHCi在运行时发现调用产生的值与函数的模式匹配不匹配时产生更好的异常消息?

它目前给出了产生非详尽模式匹配的函数的行号,虽然有时有用但需要一轮调试,有时我觉得一遍又一遍地做同样的事情.所以在我尝试整理解决方案之前,我想看看是否存在其他问题.

除了提供行号之外的异常消息还显示了它试图进行的呼叫类型?

这甚至可能吗?

haskell exception-handling exception

5
推荐指数
1
解决办法
3181
查看次数

哈斯克尔获得第一名和第七名

我需要在Haskell中编写一个函数,给定一个至少包含7个元素的列表,返回一个包含第一个和第七个元素的元组.

例如

Prelude> take1and7 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
(1, 7)
Run Code Online (Sandbox Code Playgroud)

我试过这个

take1and7 :: [a] -> (a, a)
take1and7 [a, b, c, d, e, f, g, xs] = (a, g)
Run Code Online (Sandbox Code Playgroud)

但它说它有"非详尽的模式",我不明白

syntax haskell functional-programming pattern-matching non-exhaustive-patterns

0
推荐指数
1
解决办法
95
查看次数