Assignment #6: Fun with Monads
For our final assignment in this course we’ll try something different and (I hope) a bit more fun. Your job will be to complete the server side of a little client/server implementation of the famous Cartpole problem (which some of you will soon see again in the Advanced Topics robotics course).
As you learned in CSCI-111, the client/server model is a nice way to communicate between different programs; for example, an app on your phone (client) and the server program (e.g., Instagram) that stores the data from your app. What you may not know is that the client/server model is also a standard solution to the problem you face when you need to use two different languages for the same project on your computer. In our case, the Cartpole code is provided by the Python gym package, but we want to use Haskell to write the program to solve the Cartpole task. I’ve already provide the client program client.py, which runs the Cartpole animation for you. Your job will be to complete Hw06.hs, the server program. Because networking (via sockets) is an example of a non-pure computation, for which Haskell uses monads, this exercise will give you some practice in using monads for something more interesting than file i/o.
To get started, take a look at this helpful discussion of how to use sockets in Haskell. Then open Hw06.hs and look at the comments to see what work you have to do to complete the server.
Although it should be possible to use our old friend
runhaskell to run the server code, several students found that the networking library wasn’t imported successfully that way. As a workaround, I suggest using ghc to compile an executable program for the server:
ghc -package network Hw06.hs
If ghc can’t find the package, do the following on your Mac:
cabal install --lib network
Once you’ve successfully compiled the program using ghc, just as with a compiled C program, you can run the program directly from the command line:
Then run the client in another terminal: