Cannot implement a custom operator via pattern matching

42 Views Asked by At

Basically, I need a function that accepts two lists and attempts to return a 2-list containing the head elements of both the input lists. Couple of corner cases: if both input lists are empty, the result must be an empty list. If only one list is non-empty, its head element must be returned wrapped in a list.

So far, I have tried this:

(+|+) :: [a] -> [a] -> [a]
+|+ [] [] = []
+|+ (x : _) [] = [x]
+|+ [] (y : _) = [y]
+|+ (x : _) (y : _) = [x, y]

Now, when I declare the operator as above, Haskell:

Bad operator

What am I doing wrong here?

1

There are 1 best solutions below

2
Daniel Wagner On

Operators are defined infix.

[] +|+ [] = []

If you really want to use prefix form, you can wrap it in parentheses, as needed for prefix form in other places.

(+|+) [] [] = []

For the definition, you might consider:

xs +|+ ys = [xs, ys] >>= take 1