4
\$\begingroup\$

I have made some simple functions to find the first SO chat message. How can I improve my code?

Even if there is a stackoverflow link that will get the job done (I'd be glad to know), I'd like to improve upon my already written code.

import Network.HTTP
import Text.Regex.Posix

type UserID      = String
type MessageLink = String
type PageNumber  = Integer

findLast :: UserID -> IO MessageLink
findLast = searchLast 1 "Not found" 

searchLast :: PageNumber -> MessageLink -> UserID -> IO MessageLink 
searchLast pg lnk id = do

    let link = "http://chat.stackoverflow.com/users/" ++
               id ++ "?tab=recent&page=" ++ show pg 

    c <- simpleHTTP (getRequest link) >>= getResponseBody

    let match = c =~ "/transcript/[^\"]+" :: [[String]]

    if null (c =~ "monologue" :: String)
        then return $ "http://chat.stackoverflow.com" ++ lnk
        else searchLast (pg + 1) (head . last $ match) id
\$\endgroup\$
1
\$\begingroup\$
  • Avoid partial functions like head and last.
  • http://chat.stackoverflow.com is repeated twice in searchLast. Maybe throw it into its own let-binding to make the function more DRY.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.