5
\$\begingroup\$

I'm dealing with data that stores its state as a String, treating the string like a stack. I also have to combine that with error handling.

To that end, I'm using the type StateT String Maybe a. I have a function to pop and to push a Char from and to the string:

pop :: StateT String Maybe Char
pop = do
 x:xs <- get
 put xs
 return x
push :: Char -> StateT String Maybe ()
push x = do
 xs <- get
 put (x:xs)
 return ()

I wrote a function to repeatedly pop from the string while the characters being popped fulfilled a condition. It behaves as follows:

> runStateT (popWhile (<'a')) "HELLO world"
Just ("HELLO ","world")
> runStateT (popWhile (>'a')) "HELLO world"
Just ("","HELLO world")

My implementation is the following:

popWhile :: (Char -> Bool) -> StateT String Maybe [Char]
popWhile f = do
 s <- get
 if null s
 then return []
 else popAgain
 where
 popAgain = do
 x <- pop
 if f x
 then liftM (x:) (popWhile f)
 else push x >> return []

But that seems pretty bulky, and has two if then else's in it. Is there a better way to write this function?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jan 25, 2015 at 1:15
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

You can simplify the code by using span:

span :: (a -> Bool) -> [a] -> ([a], [a])

span, applied to a predicate p and a list xs, returns a tuple where first element is longest prefix (possibly empty) of xs of elements that satisfy p and second element is the remainder of the list

popWhile :: (Char -> Bool) -> StateT String Maybe String
popWhile p = do
 s <- get
 let (xs, ys) = span p s
 put ys
 return xs

Thanks to @bisserlis for the suggestion to use state

popWhile = state . span
answered Jan 25, 2015 at 3:58
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Ah, this is exactly what I was looking for, thanks! I'm still pretty beginner when it comes to monads in Haskell, so I've a lot to learn about writing functions like this. In that sense, this is a super helpful answer, so I'll be giving this the Green Tick of Doom. \$\endgroup\$ Commented Jan 25, 2015 at 5:01
  • 2
    \$\begingroup\$ @Matthew glad I could help :) Hoogle is a very useful resource for finding functions like span. \$\endgroup\$ Commented Jan 25, 2015 at 5:05
  • \$\begingroup\$ Yeah, I've been using that lately, particularly to deal with Maybes. The trick with this though is that I need to use get and put directly and then jump into regular list functions; that wasn't obvious to me until now. \$\endgroup\$ Commented Jan 25, 2015 at 5:07
  • 2
    \$\begingroup\$ Use state :: (s -> (a, s)) -> m a to clean things up even further. I.E., pop = state uncons push = state (:) and popWhile p = state (span p). \$\endgroup\$ Commented Jan 25, 2015 at 6:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.