How To Use Triangle Operators In Pattern Matching Function

haskell, parse-error, pattern-matching, sequence

Solution

Xavier Pinho is correct. The operators `(<|)` and `(|>)` are ordinary functions, but only data constructors may be used in patterns. (Yes, they're listed in the docs under the title Construction, because they're used to construct sequences out of components, but they're not technically data constructors.)

The library offers two functions `viewl` and `viewr` to create members of datatypes `ViewL` and `ViewR`. These have constructors `:<` and `:>` that can be matched on.

An example:

s :: Seq Int
s = fromList [1,2,3]

test1 :: (Int, Seq Int)
test1 = case viewl s of
          x :< xs -> (x, xs)

test2 :: (Seq Int, Int)
test2 = case viewr s of
          xs :> x -> (xs, x)

The views are also convenient to use with the `ViewPatterns` language extension. If enabled, you can say

test3 :: Seq Int -> (Int, Seq Int)
test3 (viewl -> x :< xs) = (x, xs)

to match on an incoming sequence as if it was a list.

Problem

I'm trying to use Sequences to improve performance. When defining the function below I tried to use the "triangle" operators in pattern patching context. ``` import qualified Data.Sequence as S cleanField :: S.Seq Char -> S.Seq Char cleanField ((S.<|) ' ' xs) = cleanField xs cleanField ((S.|>) xs ' ') = cleanField xs cleanField xs = xs ``` GHC 7.4.1 says: ``` seq.hs:4:13: Parse error in pattern: (S.<|) ``` Can I not use the triangle operators (`<|`,`|>`)in pattern matching? If so, why can I use the cons (`:`) operator in pattern matching and not the triangle operators?

Original source