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. 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
); 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
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.
(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.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.