Haskell words implementation

This is my attempt of re-implementing the standard words function:

isSpace' x = x == ' '

removeFirst [] = []
removeFirst (x:xs) = if isSpace' x then removeFirst xs else (x:xs)

break' p xs = break'' p xs []
  where
    break'' p (x:xs) ys = if (p x) then (ys, x:xs) else break'' p xs (ys ++ [x])
    break'' _ []     ys = (ys, "")

words' x = words'' ("", x)
  where
    words'' (_, "") = []
    words'' (a, b) = [q] ++ words'' (q, r)
      where
        q = fst (break' isSpace' b)
        r = removeFirst (snd (break' isSpace' b))
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s