How to check for an empty intersection of lists with Haskell

haskell intersect
haskell intersection of two lists
haskell list
haskell map
haskell intersect list of lists
haskell list contains
filter haskell
haskell remove duplicates from list

Below is my code attempt for trying to create and then check the intersection of two lists in Haskell. Can someone help me get it to run?

empty_intersection :: (Eq a_ => [a] -> [a] -> Bool
empty_intersection as bs = (true if ([x | x <- as, x  `elem` bs) else false)
Problem

Your code will not compile because a list cannot be a boolean, so you cannot have it in an if statement. There are however functions like all, any and null that can take lists and return booleans.

Solution

Just use the Prelude function null :: [a] -> Bool which returns true if your list is empty. (Also note as @Bergi said you have a typo in your type constraint)

empty_intersection :: (Eq a => [a] -> [a] -> Bool
empty_intersection as bs = null [x | x <- as, x  `elem` bs]
Even better

Also, as it's your second question on set unions and intersections, note that there is a library Data.Set for dealing with ordered sets which has much more efficients functions for intersection, union, membership, difference, etc.

Data.Set - Hackage, Functions that take two sets as arguments and combine them, such as union and Check whether two sets are disjoint (i.e., their intersection is empty). disjoint� Haskell has a function called filter which will do this for you. Beware though: it should really be named 'select' instead. For example, filter odd xs returns a list of odd numbers. That is, it deletes everything that is not odd. Testing various conditions. Check if a list is empty. null xs. Find out whether any list element passes a given test

intersect - Hoogle, Search plugin; Manual � haskell.org � Hoogle. set:stackage, set:haskell-platform, set:included-with-ghc, package:abstract-deque, package:abstract-par� Left-associative fold of a structure. In the case of lists, foldl, when applied to a binary operator, a starting value (typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:

You can use intersect

empty_intersection :: (Eq a) => [a] -> [a] -> Bool
empty_intersection = null . intersect

Introduction To Haskell Checking Subsets, Introduction To Haskell Checking Subsets Here are two functions that test with set a is a subset of set b. subset a b If this list is empty, then a is a subset of b. Intersection, union and difference are the operations that you want to work out. Left-associative fold of a structure. In the case of lists, foldl, when applied to a binary operator, a starting value (typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:

set intersection of two lists, Haskell, 45 42 bytes First the intersection is computed, possibly with duplicates . This is This script does not sort and makes sanity check if first set is empty. The intersection of two sets. Elements of the result come from the first set, so for example. import qualified Data.Set as S data AB = A | B deriving Show instance Ord AB where compare _ _ = EQ instance Eq AB where _ == _ = True main = print (S.singleton A `S.intersection` S.singleton B, S.singleton B `S.intersection` S.singleton A)

Sets — containers 0.5.10.2 documentation, fromList ["Set", "Map", "Graph", "Sequence"] -- Check if "Map" and "Trie" are in the set of data structures. When using Set or IntSet in a Haskell source file you should always use a qualified import empty creates a set with zero elements. intersection returns a set the elements that are in both sets l and r (set intersection). O(log n).Combines insert operation with old value retrieval. The expression (insertLookupWithKey f k x map) is a pair where the first element is equal to (lookup k map) and the second element equal to (insertWithKey f k x map).

Modules, The syntax for importing modules in a Haskell script is import <module name>. If you look at a list of lists as a 2D matrix, the columns become the rows and vice versa. Much like a list can be either an empty list or a list with some elements, The nub, delete, union, intersect and group functions all have their more� Intersection of two lists - not possible without dirty tricks? I was thinking about this problem and the solution outlined therein and think that you wouldn't be able to do this in Haskell because you can only get value equality.

Comments
  • (Eq a_ should be (Eq a)
  • … if … else … is not Haskell syntax. en.wikibooks.org/wiki/Haskell/Control_structures
  • Big thanks to Lorenzo and @Robin-Zigmond for getting me on track with the syntax. There was one more error that had to be corrected as well as shown in the edit above but its working perfectly.
  • Thanks @Netwave - I just copied it from the OP and didn't even notice that! I see the OP also corrected it though.