Skip to content

Instantly share code, notes, and snippets.

@crabmusket
Last active December 24, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crabmusket/6883758 to your computer and use it in GitHub Desktop.
Save crabmusket/6883758 to your computer and use it in GitHub Desktop.
Working on a function to receive a line of serial data in Haskell.
import qualified Data.ByteString.Char8 as B
import System.Hardware.Serialport (SerialPort, recv)
recvLn :: SerialPort -> IO B.ByteString
recvLn s = do
-- Receive one byte at a time.
first <- recv s 1
rest <- if first == B.singleton '\n'
then return $ B.empty
else recvLn s
-- Once the recursion finishes, we glue the parts together.
return $ first `B.append` rest
@crabmusket
Copy link
Author

Biggest concern right now is that it doesn't appear very lazy. I'm not sure how the compiler will actually shake it out, but in pureland some sort of takeWhile seems like the best solution. Not sure if that will be applicable here.

But it works!

@danbst
Copy link

danbst commented Oct 10, 2013

too many coments!

-- If the byte was a newline,
rest <- if first == B.pack "\n"

These lines are just the same, why write it twice? Other are also too obvious. Try to document nuances, not the codeflow.

@crabmusket
Copy link
Author

@danbst I agree. I was initially worried about having no comments. I think I've struck more of a balance now! The first one may seem obvious, but I'm thinking of people who aren't familiar with the serialport library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment