December 9, 2015 | Article | No Comments
Haskell is one of powerful functional programming language. More specific, it is a polymorphically statically typed, lazy, purely functional language. It is the good language for expressing math equation and logic naturally. Haskell is based on the lambda calculus, thus it use lambda as the logo.
Despite of functional paradigm nature, Haskell is very good for network and distributed system application. It can also applied as application on cloud computing.
After installing haskell on Raspberry Pi, it is worth to test what Haskell can do. This article will discuss about creating a simple Echo Server on Raspberry Pi using Haskell programming language.
In this article I use:
- Raspberry Pi model B 512MB RAM
- Raspbian Wheezy 2013-07-26 hardfloat
What we really want to build in this article is a modified Echo server. This program will run forever (infinite loop) and listen on port specified by user when invoking program. Like any socket-based application, when a connection come, it will accept and process it according to our app’s logic. Our logic is so simple. Whenever user connect to server using telnet, this server will echo every line they write (echo). However when they write “ping” without quote, the server will reply with “pong”.
Make sure you have install Haskell on your Pi.
Writing code to Pi can be done using any method. You can access your Pi remotely or access directly. At least, make sure you can create a file and write it.
Writing the Code
Write this snippet and save it as “echoserver.hs”:
import System.Environment (getArgs, getProgName) import Control.Concurrent (forkIO) import Network (Socket, PortID(PortNumber), withSocketsDo, listenOn, accept) import System.IO (hPutStrLn, hGetLine, hFlush) import Control.Monad (forever) main = withSocketsDo $ do args <- getArgs prog <- getProgName case args of [port] -> do socket <- listenOn $ PortNumber (fromIntegral (read port :: Int)) putStrLn $ "Listening for pings on " ++ port handleRequest socket _ -> putStrLn $ "usage: " ++ prog ++ " <port>" handleRequest s = do (handle, _, _) <- accept s forkIO $ forever $ do input <- hGetLine handle processPing (hPutStrLn handle) (init input) hFlush handle handleRequest s processPing f msg = do if msg == "ping" then f "pong!" else f msg
Also, make sure you pay attention to the indentation.
This code is simple and should run on any architecture supported by GHC, including our Pi.
Build and Testing
To build the above code, run this command (assume the code is saved as echoserver.hs):
ghc -O2 --make echoserver.hs
The -O2 flags are used to apply every non-dangerous optimization, but might make the compilation time longer.
After compilation successful, you will have a new file name echoserver. To run the server do this on your Pi:
This will run a server and make it listen to port 5000.
Now on your local computer, open up command prompt / terminal and invoking telnet to connect to your Pi. Suppose our Pi is on address 192.168.1.11, do following:
telnet 192.168.1.11 5000
which will connect us to Pi on port 5000.
You can test by write any line there. Good luck 🙂