CamelBak
CamelBak

Reputation: 105

Creating an infix function

I am currently trying to create an infix function of |= ( Every A is B, Syllogism ).

(|=) :: Predicate Thing -> Predicate Thing -> Bool
a |= b = [ if a == b then True | x <- a , y <- b ]

The problem is I am getting an parse error on input |

Any Help would be Appreciated !

Upvotes: 0

Views: 391

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477794

The problem is the list comprehension. You can not write an if … then …. An if … then … else … is an expression, not a statement.

If you want to return True if one of the elements of a is equal to one of the elements of b, you can work with or :: Foldable f => f Bool -> Bool:

(|=) :: Predicate Thing -> Predicate Thing -> Bool
a |= b = or [ a == b | x <- a , y <- b ]

or you can work with any and elem:

(|=) :: (Foldable f, Foldable g, Eq a) => f a -> g a -> Bool
a |= b = any (`elem` b) a

This however only works if Predicate is list, or at least a Foldable in the second case. Not for a custom type (that is not an instance of Foldable).

Upvotes: 1

Related Questions