user14599042
user14599042

Reputation:

Is it possible to rewrite this Haskell function using guards with no case-of?

I have this Haskell function that uses "case of", but I am trying to figure out if it's possible to rewrite this function using guards("|") and not "case of". Is it possible ? Any suggestion would be helpful.

lefQ :: Ord a => LOL a -> Lang a -> Lang a
lefQ (LOL i xs) [] = []
lefQ (LOL i xs) (LOL j y:ys) = case stripPrefix xs y of
                    Nothing -> leftq (LOL i xs) ys
                    Just zs -> (LOL (j-i) zs):lefQ (LOL i xs) ys

Upvotes: 2

Views: 128

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476709

You can work with a pattern guard, so:

lefQ :: Ord a => LOL a -> Lang a -> Lang a
lefQ (LOL i xs) [] = []
lefQ (LOL i xs) (LOL j y:ys)
    | Just zs <- stripPrefix xs y = (LOL (j-i) zs): lefQ (LOL i xs) ys
    | otherwise = leftq (LOL i xs) ys

Upvotes: 4

Related Questions