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
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Aug 18, 2015 at 22:29
\$\endgroup\$
1

1 Answer 1

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.
answered Jan 22, 2017 at 23:42
\$\endgroup\$

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.