9
爲了學習的目的,我試圖編寫我自己的zipWith
函數的實現。但是,我遇到了與_
邊緣案例模式匹配的問題。首先我要描述好的情況,然後是壞的情況。希望有人能夠解釋他們爲什麼會有不同的表現。由於使用通配符進行模式匹配時'錯誤的多重聲明'
如果我寫的zipWith
功能如下,它的工作原理(注的邊緣情況匹配上線2空單& 3的順序): -
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipwith' _ [] _ = []
zipWith' _ _ [] = []
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
編譯在GHCI: -
ghci> :l ZipWith.hs
[1 of 1] Compiling Main (ZipWith.hs, interpreted)
好,上面是細的,但如果我換周邊GHCI邊緣的情況下的圖案匹配拋出錯誤「的多個聲明」的線2和4
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ _ [] = []
zipwith' _ [] _ = []
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
編譯在GHCI: -
ZipWith.hs:4:0:
Multiple declarations of `Main.zipWith''
Declared at: ZipWith.hs:2:0
ZipWith.hs:4:0
Failed, modules loaded: none.
我難倒...
- 看着上線2和4的模式,他們似乎相互排斥的,但我顯然失去了一些東西這裏的基礎
- 爲什麼切換第2行和第3行的模式會導致編譯錯誤消失。
哦,我錯過了錯字,因爲我發現它很尷尬,感謝您發現它並解釋我錯誤的解釋。 – Jabbslad 2011-01-31 22:09:07