作为一个练习,我试图手动实现前奏中有趣的部分。每当我发现有机会免费得分时,我都会抓住它。然而,这让我在最不可能的地方找到了一堵砖墙。使用此代码:
myelem _ [] = False
myelem x y = if x == head y then True else myelem x (tail y)
我正在尝试实施notElem
. 这是我的尝试:
-- First
mynotelem = not myelem
由于类型不匹配而爆炸是可以理解的。这很容易解决:
-- Second
mynotelem x y = not (myelem x y)
然而,参数 x 和 y 的显式声明感觉丑陋且不必要,所以我尝试将其恢复为无点样式。
-- Third
mynotelem = not $ myelem
哪个失败了
Couldn't match expected type `Bool'
with actual type `a0 -> [a0] -> Bool'
In the second argument of `($)', namely `myelem'
In the expression: not $ myelem
In an equation for `mynotelem': mynotelem = not $ myelem
很公平,类型仍然不匹配。但是你如何解决它?再次,您可以直接跳转到
-- Fourth
mynotelem x y = not $ myelem x y
哪个有效,但似乎危险地接近于绕圈子。我发现可以消除其中一个论点:
-- Fifth
mynotelem x = not . (myelem x)
但是那个讨厌的 x 仍然存在。我该如何消除它?