Pattern matching `zero`

#1

I’m working through Chapter 5 of the PureScript book and want to make lzs (longest zero suffix) more general. I came up with the following:

lzs :: forall a. Semiring a => Array a -> Array a
lzs [] = []
lzs xs = case sum xs of
           zero -> xs
           _ -> lzs (fromMaybe [] $ tail xs)

The zero is being interpreted as a new variable with the name zero instead of the zero from Prelude. Is there a way to tell PureScript to use the zero from the global scope instead of creating a new variable?

#2

You need an Eq constraint to do this. Pattern matching syntax doesn’t incur constraints.

lzs :: forall a. Eq a => Semiring a => Array a -> Array a
lzs [] = []
lzs xs | sum xs == zero = xs
       | otherwise = lzs (fromMaybe [] $ tail xs)
1 Like