简体   繁体   English

Haskell 搜索元组列表

[英]Haskell search list of tuples

I'm trying to make a programme that searches through a list of tuples (Char, Bool) and returns true if there are any two conflicting elements within it.我正在尝试制作一个程序来搜索元组列表(Char, Bool) ,如果其中有任何两个冲突的元素,则返回 true。 Two tuples are conflicting if they have the same value for the first element ( Char ) in the tuple but the second ones are different ( Bool );如果两个元组在元组中的第一个元素( Char )具有相同的值但第二个元素不同( Bool ),则它们是冲突的; eg, eg:例如,例如:

[('a', True),('a', False)] returns False , 'a'='a' but True != False
[('a', True),('a', True) returns True. , 'a'='a' and True=True
ListofTupels :: [(a,a)] -> [a]
type ListofTupels = [(Char,Bool)]

The function is defined like this function 定义如下

searchValidity :: ListofTupels -> Bool

does anyone have any ideas how to do this?有谁知道如何做到这一点?

Use recursion.使用递归。 An empty list is vacuously valid:空列表是无效的:

searchValidity :: ListOfTupels -> Bool
searchValidity [] = True

An non-empty list consists of a valid tail and a head that doesn't invalidate the list.一个非空列表由一个有效的尾部和一个不会使列表无效的头部组成。

searchValidity ((c, b):rest) = searchValidity rest && ...

I leave filling in ... as an exercise.我把填写...作为练习。 The lookup function in the Prelude should be useful. Prelude 中的lookup function 应该很有用。

(I leave it as a further exercise to do this in linear, rather than quadratic, time. Using lookup at each step slows this down greatly.) (我将其作为进一步的练习,以线性时间而不是二次时间来执行此操作。在每个步骤中使用lookup会大大减慢速度。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM