Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern-matching Seq's in Haskell

Pattern matching is one of the most elegant Haskell features.

I've been working on a project recently where I need a queue data structure so I'm using Data.Sequence. However, it looks like I have to give up the elegance of pattern matching and resort to guards:

floodFillWorker :: Image -> RGBAColor -> Double -> PixelQueue -> Image
floodFillWorker image base tolerance queue 
    | Seq.null queue = image
    | otherwise      = doSomeWork image

Can I use pattern matching with sequences or do I need to use guards?

like image 414
Bill Avatar asked Jan 16 '10 16:01

Bill


1 Answers

ephemient is on the right track with view patterns but I think there's a way to do it which is actually quite nice. Data.Sequence was actually written with views in mind and you should use either the ViewL or ViewR types in order to patternmatch on the data structure.

{-# LANGUAGE ViewPatterns #-}

floodFillWorker image _ _ (Seq.viewl -> EmptyL) = image
floodFillWorker image base tolerance queue = doSomeWork image
like image 79
svenningsson Avatar answered Nov 05 '22 18:11

svenningsson