Get Started with Sending SMS in Haskell

Functional programming languages are gaining in popularity so let’s hop on that Haskell bandwagon. With Haskell, you can write clean, concise, and expressive code. Don’t believe me just yet?

Java program versus Haskell program
The Java and Haskell programs above make two lists: one of all even numbers up to 100, and one of the those same even numbers but without the first five numbers. Which one looks nicer?

Now let’s dive into setting up your Haskell developer environment by

  1. configuring two package managers
  2. installing Haskell
  3. making a “Hello, World” program
  4. sending our first SMS with Twilio using Haskell (get a Twilio phone number here)

Environments

There is no complete Haskell IDE (so if you want to make one, the market is wide open.) Many Haskellers use Atom because it has the language-haskell and ide-haskell packages, whose syntax-highlighting of aspects like import statements make it stand out. They also help with auto-indentation, auto-completion, and identifying types.  My Haskell professor recommends Atom with Emacs which works in terminal and has good syntax highlighting and code-completion. For you Vim fans, never fear: you can set up Vim and Haskell like this. You can also try Eclipse with this colorer plugin or Leksah which is written in Haskell, uses the cabal package format, and offers multi-window support. I however prefer Sublime Text 3 mainly because I’m used to it for web development. It offers a SublimeHaskell package with smart completion, type inference, and error and warning highlighting, and is what was used to make this post.

Install Haskell

The interactive Glasgow Haskell Compiler is all open source on GitHub. Install both Haskell and GHC here from a Mac or visit here from a PC. To test it run $ghci on the command line. You should see something like this if all goes well.

Successful Haskell Installation on the Command Line

 

Finding and Downloading Packages

A package is a library of Haskell modules that the compiler recognizes. These modules contain functions and types that can help you solve problems when coding, letting you work with REST APIs (hey, like Twilio!) Where can you find neat Haskell packages to code with? Check out Stackage and Hackage for open-source packages which you download using the stack and cabal package managers. The main difference between the two is that Stackage only offers a subset of the packages Hackage offers. Hackage packages could potentially have dependency issues, but most are completely fine out-of-the-box. Overall, Hackage offers a greater spread and variety to choose from.

Stack

Stack is my preferred package manager for Haskell. Stack is isolated; so there are no changes made outside of specific Stack directories when you stack install on the command line. When you build with stack, it makes reproducible builds (also known as package caching) so that stack build does the same thing tomorrow as it does today if none of the code changes. Stack also offers a nice, clean developer experience.

On a Mac machine, type the following on the command line to install:

If you’re using Windows, install Stack with this Windows 64-Bit Installer. For more details on Stack, visit their install/update page.

Stack is very nice with setting up your initial project. This next section is run in the terminal and shows how you would get started with a new stack project. Replace anything starting with “your with a name you choose.

Cabal

The other widely-used Haskell package manager is Cabal. It is included in the Haskell platform which sort of makes you think it would be easier to use…I WISH! Honestly? Many Haskellers I’ve met are not big fans of cabal. My biggest problem with Haskell has been issues with cabal dependencies (also known as cabal hell). What’s the solution to that problem? Sandboxes, which allow developers to isolate and build packages by making a private package environment for each package like in Python’s Virtualenv.

To install cabal, run in the terminal

Getting Hello World Up and running

To get Hello World, no packages are needed. On the command line, type ghci and after clicking return, type

But *gasp* that’s so fast.

Rocky fast run

How about making “Hello, World” from a Haskell file? Make a new directory to put your project in and call it sms. Let’s make a file in it called first.hs and on the first line write

Switching back to the command line, type the following to compile your file.

Then run the executable hello by typing on the command line:

Voila! You just completed Hello, World two ways in Haskell.

Get Started with Twilio in Haskell

First, get a Twilio phone number that can receive text messages. You can do that here if you don’t have one already.

You’ll need your Twilio Account SID and Auth Token to send texts. Grab those from your console, and store them as environment variables. If you’re on a Windows-based system, see this post to do that. In a unix-based system, you would set them up by typing the following two lines in your shell and add them to your  .bash_profile.

On the command line in your sms directory run stack new sms.  This will set up our project with files like sms.cabal, stack.yaml, Setup.hs, and Main.hs. In the file sms.cabal, find the line which begins with build-depends of the executable and remove the line beginning withbuild-depends which is there. Put the following in that space instead to use Twilio:

Next on the command line type stack init --solver so that stack can determine a package set compatible with your sms.cabal file. Following that, in whichever IDE you would like (see above thoughts on that), open up the directory you just made.

Now open up Main.hs (located in the app folder) and erase whatever code is already there. In its place type the following code. Because we’re sending our first SMS, update the following with the phone numbers you wish to send a SMS to and your own Twilio phone number:

Open up a fun Hacker News article to read because this next step will take a little bit of time. On the command line, type

Waiting Silicon Valley gif

Once it’s done, let’s run the executable by typing on the command line

SUCCESS! You should have just sent a SMS message in Haskell and now see this in your terminal:

Success SMS sent message in terminal

If you get an error like“getEnv: does not exist (no environment variable),” make sure your environment variables are set correctly and check your corresponding variable names. Then open up a terminal window and run

Conclusion

Want to learn more about Haskell in general? I recommend Learn You a Haskell and O’Reilly’s Real World Haskell. Stay tuned for more Haskell posts to learn more about this functional language, and let me know what you think of any IDEs, cabal or stack, or Haskell in general online.

Email: lsiegle@twilio.com

GitHub: elizabethsiegle

Twitter: @lizziepika