Home > Software design >  Haskell check if a list of lists has an empty list
Haskell check if a list of lists has an empty list

Time:10-04

I have tried to create a function that determines if a list of lists has an empty list. However so far I don't seem to find any luck. So far I have tried using: hasEmpty (x:xs) = if null x then True else False However this only return true if the first list is empty. I also tried: hasEmpty (x:xs) = if null x || null xs then True else False But it produces the same result. I have also tried using any and elem but I couldn't get it working. I am truly stumped on this. Any help would be much appreciated

CodePudding user response:

The type of any is any :: Foldable t => (a -> Bool) -> t a -> Bool (use :t any) to get this.

There are two arguments:

  1. The first argument is a function which takes a value and returns a boolean, such as null
  2. The second argument is a foldable, such as a list

Hence we can simply use any null on a list.

lst = [[1],[],[3]]
lst2 = [[1],[3],[2]]

any null lst -- returns True
any null lst2 -- returns False
  • Related