检查列表其余部分中的元素(lx)(Check for an element in the rest of the list (lx))

编程入门 行业动态 更新时间:2024-10-17 11:29:21
检查列表其余部分中的元素(lx)(Check for an element in the rest of the list (lx))

我最近选择了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 = True

The 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 lx

Eq是一个类型类,它意味着在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 _ _ = True

You 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 lx

You 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 lx

Eq 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

更多推荐

本文发布于:2023-08-07 01:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459049.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   列表   lx   Check   rest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!