Haskell(n + 1)进行模式匹配 [英] Haskell (n+1) in pattern matching
问题描述
当我遇到 Haskell中的99个问题时, //haskell.org/haskellwiki/99_questions/Solutions/19rel =noreferrer>解决方案至问题19 ,我没有完全理解。
任务是编写一个像这样工作的旋转函数。 b
*主要>旋转['a','b','c','d','e','f','g','h'] 3
defghabc
*主>旋转['a','b','c','d','e','f','g','h'](-2)
ghabcdef
一个提供的解决方案是
rotate [] _ = []
rotate l 0 = l
rotate(x:xs)(n + 1)= rotate(xs ++ [x])n
rotate ln = rotate l(length l + n)
我不明白模式匹配如何达到第四行。它似乎与(n + 1)
有关,因此当 n
为负值时,第三行不会匹配,因此第四个被采取。如果是这种情况,为什么符号(n + 1)
会以这种方式工作。不是那种武断的,或者是我没有意识到的习惯(数学?)?
因为我理解的方式是在第三行中递归地调用rotate,参数 n
减少了一。所以我会认为:
rotate [] _ = []
rotate l 0 = l
rotate (x:xs)n = rotate(xs ++ [x])(n-1)
rotate ln = rotate l(length l + n)
是等价的。但事实并非如此。此定义给出以下警告:
pre $ 警告:模式匹配重叠
在`rotate'的定义中, :旋转ln = ...
而前面的定义编译得很好。
这是所谓的n + k模式的一个特例,它通常是不喜欢的,而 将从语言中删除。请参阅此处获取更多信息。
此处是关于n + k模式的好消息,它引用以下从Haskell 98报告(重点是我的):
匹配n + k模式(其中n是
变量和k是一个正整数
文字)对于一个值v ,如果
x> = k 成功,导致n
与x-k的绑定,否则失败。同样,
函数> =和 - 被重载,
取决于模式的类型。
如果比较
发散,则匹配发散。
字面值k的解释是
,与数字字面值$ b相同$ b模式,只允许使用整数
文字。
所以 n + 1如果您怀疑
至少为1,那么只会匹配。您的替代代码将删除此限制,从而导致模式匹配重叠。
I was doing the 99 Problems in Haskell when I encountered a solution to Problem 19 that I did not fully understand.
The task is to write a rotate function that works like this
*Main> rotate ['a','b','c','d','e','f','g','h'] 3
"defghabc"
*Main> rotate ['a','b','c','d','e','f','g','h'] (-2)
"ghabcdef"
One provided solution is
rotate [] _ = []
rotate l 0 = l
rotate (x:xs) (n+1) = rotate (xs ++ [x]) n
rotate l n = rotate l (length l + n)
I don't understand how the pattern matching can ever reach the fourth line. It seems to have to do with the (n+1)
so that when n
is negative the third line does not match and therefore the fourth is taken. If that is the case why does the notation (n+1)
work that way resp. isn't that arbitrary or is that a convention (in mathematics?) that I'm not aware of?
Because the way I understand it is that rotate is called recursively in the third line with the argument n
reduced by one. So I would think that
rotate [] _ = []
rotate l 0 = l
rotate (x:xs) n = rotate (xs ++ [x]) (n-1)
rotate l n = rotate l (length l + n)
is equivalent. However, it is not. This definition gives the following warning
Warning: Pattern match(es) are overlapped
In the definition of `rotate': rotate l n = ...
whereas the former definition compiles just fine.
It's a specific case of what is called "n+k patterns", which is generally disliked, and will be has been removed from the language. See here for more information.
Here is a good note on n+k patterns, which quotes the following from the Haskell 98 Report (emphasis mine):
Matching an n+k pattern (where n is a variable and k is a positive integer literal) against a value v succeeds if x >= k, resulting in the binding of n to x - k, and fails otherwise. Again, the functions >= and - are overloaded, depending on the type of the pattern. The match diverges if the comparison diverges.
The interpretation of the literal k is the same as in numeric literal patterns, except that only integer literals are allowed.
So the n+1
is only matched if n
is at least 1, as you suspected. Your alternative code removes this restriction, resulting in overlapping pattern matches.
这篇关于Haskell(n + 1)进行模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!