我最近选择了Haskell,我无法在代码中查看元素是否在列表的其余部分(x:lx)在本例中为lx 。 我的代码:
atmostonce:: [Int] -> Int -> Bool atmostonce [] y = True atmostonce (x:lx) y | (x==y) && (`lx` == y) = False | otherwise = True它现在的方式是检查第一个元素(x==y)但我不知道如何检查元素y存在于lx 。 我实际上要完成的事情是找出是否在Intigers lx列表中,数字y包含0或1次并返回True否则返回False
i recently picked up Haskell and i am having trouble putting in code the way to look if an element is in the rest of the list (x:lx) in this case in lx. My code:
atmostonce:: [Int] -> Int -> Bool atmostonce [] y = True atmostonce (x:lx) y | (x==y) && (`lx` == y) = False | otherwise = TrueThe way it is now checks for the first element (x==y) but i don't know how to check if the element y exists in lx. The thing i am actually trying to accomplish is to find out if in the list of Intigers lx the number y contains 0 or 1 times and return True otherwise return False
最满意答案
您可以使用elem函数执行此操作:
atmostonce:: [Int] -> Int -> Bool atmostonce [] y = True atmostonce (x:lx) y | x /= y = atmostonce lx y | otherwise = not $ elem y lx最好先检查元素x是否不等于y 。 如果是这种情况,你只需调用递归部分atmostonce lx y :你因此在列表中进一步搜索。
如果x == y ,(在otherwise情况下),您需要检查lx是否有另一个元素(列表的其余部分),等于x 。 如果是这种情况,则需要返回False ,因为在这种情况下,列表中有多个实例。 否则你返回True 。
此外,您可以进一步概括您的功能:
atmostonce:: (Eq a) => [a] -> a -> Bool atmostonce [] y = True atmostonce (x:lx) y | x /= y = atmostonce lx y | otherwise = not $ elem y lxEq是一个类型类,它意味着在a定义a函数==和/= 。 所以你可以调用它们,而不管它们的真实类型( Int , String ,等等)。
最后在第一种情况下,您可以使用下划线( _ ),这意味着您不关心该值(尽管在这种情况下无关紧要)。 你也许可以改变案例的顺序,因为它们是分离的,这使得函数在语法上是完全的 :
atmostonce:: (Eq a) => [a] -> a -> Bool atmostonce (x:lx) y | x /= y = atmostonce lx y | otherwise = not $ elem y lx atmostonce _ _ = TrueYou can do this using the elem function:
atmostonce:: [Int] -> Int -> Bool atmostonce [] y = True atmostonce (x:lx) y | x /= y = atmostonce lx y | otherwise = not $ elem y lxYou better first check if the element x is not equal to y. If that is the case, you simply call the recursive part atmostonce lx y: you thus search further in the list.
In case x == y, (the otherwise case), you need to check if there is another element in lx (the remainder of the list), that is equal to x. If that is the case, you need to return False, because in that case there are multiple instances in the list. Otherwise you return True.
Furthermore you can generalize your function further:
atmostonce:: (Eq a) => [a] -> a -> Bool atmostonce [] y = True atmostonce (x:lx) y | x /= y = atmostonce lx y | otherwise = not $ elem y lxEq is a typeclass, it means that there are functions == and /= defined on a. So you can call them, regardless of the real type of a (Int, String, whatever).
Finally in the first case, you can use an underscore (_) which means you don't care about the value (although in this case it doesn't matter). You can perhaps change the order of the cases, since they are disjunct, and this makes the function syntactically total:
atmostonce:: (Eq a) => [a] -> a -> Bool atmostonce (x:lx) y | x /= y = atmostonce lx y | otherwise = not $ elem y lx atmostonce _ _ = True更多推荐
发布评论