Tuesday, May 21, 2013

How to Learn Haskell

For whatever reason, Haskell's curve of interest has been going up again. Haskell is a philosophy as much as a language, and a controversial one at that. Haskell is lazy and purely functional language. I will explain what each of those are at the bottom of this article. This is more specifically about how to set up a good Haskell learning environment.

Learning Material
For learning Haskell, look no further than http://learnyouahaskell.com/. Learn You a Haskell is written in a similar manor to Land of Lisp with its quirky prose and downright hilarious examples. Due to Haskell's lack of wide use, this book has a near monopoly on the task of learning Haskell; which is fine. The book covers everything very in-depth. If there is something in here, you will need it to use Haskell effectively, so do not skip around, especially when the problems gets harder (like the RPN calculator). Extra learning material and support can be found in the sidebar of http://www.reddit.com/r/haskell/

Development Tools
Haskell is nothing without a good text editor. There are some relatively advanced formatting standards within Haskell (especially with guards: see below). The best editor for Haskell is subjective, but I will cover the tools that I have used for Haskell development and try to take an unbiased look at each one. Before reading the recommendations I have to come clean; I use Emacs, so I may lean towards it a little bit and compare a lot of the tools to Emacs.

     Eclipse FP
Eclipse FP is a plugin for the Eclipse framework that provides Haskell project support. Extra goodies can be installed via cabal (the haskell "package manager"). If you can get it working on your system with all of the recommended goodies (scion for syntax highlighting, ghc-mod for ghci integration, etc) then this could be a very easy and welcoming development tool. Drawbacks are the chance that it will NOT work (it didn't for me, but this was the first tool I tried to use. It's probably very good) and having to install Java as well as Haskell (~128 MiB in total).

Leksah ("Leck - Sah", Haskell backwards) is  full-blown Haskell IDE written in yours truly (Haskell). It has some interesting features, like listing the type signature of a function when you type it (think: Javadoc lookup in other IDE's) and trying to compile the file on every keystroke, showing you (and underlining) when you make a mistake. It supports editing of Cabal files, project browsing, and graphical integration with the Haskell debugging tools. For serious Haskell users, consider Leksah. I found that it is too early in its development to be useful to me (I experienced several crashes without file saves, but this may not happen to you), but the amount of tools this brings into one project makes it enticing if you really want to get your hands wet with Haskell. Since this is a post about learning material, I don't think that this would be appealing. 

One many in my IRC channel said that Emacs is "Not an editor, but an operating system with a [censored] good editor". Emacs has been around for a long time and is very extensible, which means it has a lot of tools written for it. This is perfect, because Haskell matches this description very well. To make Emacs useful with Haskell, one first must install el-get  and then use el-get to install haskell-mode, ghc-mod, and scion. Be sure to add the appropriate code into your .emacs file. 

The benefits to using this is that C-c C-l will load a file into a ghci minibuffer, which is great for testing out programs on the fly as well as saving them for compilation later. Yes, Eclipse has features like this, and so does vim with the right plugins, but Emacs and these extensions happened to hit my sweet spot. Look around and see what is right for you. in this department. Emacs can also extend into an IRC client, so asking for help on #haskell on Freenode is very simple.

Learning Strategy
Haskell is overwhelming to many. If you do not know a functional language already, it may even be wise to learn a bit of Common Lisp before using Haskell. Haskell is purely functional. There are no loops or IO outside of the IO Monad, so it is a major forced paradigm shift. I would learn enough Haskell from "Learn You A Haskell" to be able to work on projects (pattern matching, functors, etc), work on some, and go back to where you left off to finish up your learning. Learn You A Haskell is a very in-depth book and without the context of some projects it will all get jumbled.

Wednesday, May 8, 2013

5 Great Linux command line Programs

Linux has withstood the test of time and gathered an interesting repertoire of software over the years. This is a list that moves from my most used to my least used software on my system. Some of the tools here are widely used and advanced, but if you aren't already using them they're worth a gander.

1. tmux
Ah, where would any of the experienced linux users be without tmux or GNU/Screen? Tmux is a terminal multiplexer, meaning that it can split a terminal session into multiple panes that can be navigated with the keyboard.  The panes can be resized and sent to windows (like graphical workspaces). The best feature is that this is client-server software, and if you close the window where you are running tmux it still won't die. For persistent ssh connections or IRC chats, tmux is king in the command line.

2. ssh
ssh is the secure version of telnet. It allows a user that is using openssh-server on their host machine to connect remotely from the command line to perform tasks on the host machine. If you are using X as your display server, you can even forward graphical applications over ssh or access the filesystem with sshfs.

3. git
Git is the version control programmed by Linus Torvalds, the same guy that wrote the Linux kernel. Git allows a user to keep snapshots of changes of a project on their computer. git is SCM and not very useful for binary data, but it works wonders for projects that need collaboration or just a very secure way to roll back changes. Git will use all available cores for compression, making it the fastest SCM in existence. Git is supported by websites like Google Code and Github.

4. vim
Vim is Vi-Imporoved. For readers who are unaware, vi is the standard Unix editor. If you have a Linux system, you have vi (this is even true for Android). Vim improves on vi extensively, adding syntax highlighting, improved navigation, and extensive plugin support. Even though graphically I prefer emacs, for remote sessions or quick edits nothing beats Vim. What sets vim apart from Emacs is modal editing, where the user has two modes to edit the file in.

5. mplayer
Last but not least we have mplayer. mplayer (and mplayer2) are tools for the command line that can play video and music of an incredible amount of formats. mplayer will use X to draw a window for a video, but it can play video in text completely with ASCII animations.

Using Maybe in Haskell

Haskell is a quirky language to say the least. I've been playing around with it for about a month now. Today somebody pointed out that I was "doing it wrong" with one of my functions. I had it defined as (roughly)

pos :: (Eq a) => [a] -> a -> Int
pos [] _ = 0
pos a@(x:xs) y
  | (y `notElem` a) = -1
  | x == y = 0
  | otherwise = 1 + (pos xs y)

The obvious problem with this is that if y is not a part of the list (and yeah, it will check every time, also bad) it returns -1. -1 is a hack in a lot of procedural languages that means "Woah there nelly, something went wrong". In Haskell it doesn't have to be this way.

What Maybe is
Maybe is a type with two type constructors (Just and Nothing). Just uses a type variable whereas nothing has no parameters. Either the thing is returned or it is not, end of story.

Here is a function that returns an int if the number parameter is less than 10, nothing otherwise.
test :: Int -> Maybe Int
test x
  | x < 10 = Just x
  | otherwise = Nothing

Remember that Just and Nothing are not types, but rather they are type constructors, which is why we can put them in the type signature under "Maybe".

How to use Maybe values
To "extract" a maybe value, you either have to make the function accept a maybe or extract the value beforehand.

Extracting the Maybe Value
Here is an extracting function that if it is a Maybe of a it will return the a, but otherwise unit (Won't actually work; just case in point)

extract :: Maybe a -> a
extract (Maybe a) = a
extact (Nothing) = ()

Make one of these extracting functions for whatever type you think you will be using.

To destructure a maybe within the function, just include the type constructor like was done in extract. This is what pattern matching was made for.

Monday, May 6, 2013

All about i3 - A tiling window manager review

i3 demo:

I have long been a user of tiling window managers. They provide a distractions-free environment, and as my work-load has been increasing, so has the number of distractions that the Linux desktop can throw at me. Sometimes it's a good thing to shut everything out and get to computing.

The number of frills that i3 comes with is SO low in fact, that it doesn't even offer a clock! When you log in you will see a minimalist interface kind of like this (minus the bar at the very bottom, that was my idea and I will cover that later)
I didn't know where to start. I couldn't do anything, and it is all my fault. I hadn't researched i3 at all. I hadn't the slightest clue how to open a terminal to "man i3" or use "google-chrome". Worse yet, I didn't even know how to exit the session, so I had to escape to a tty and pkill gdm. I was not liking i3.

It is very important to realize that i3 has a VERY steep learning curve that will gobble you up if you don't put in some effort. The end-all-be-all guide to i3 is http://i3wm.org/docs/userguide.html, the i3 user guide. It will instruct you on everything you could possibly want to do with i3. It explains the philosophy behind the window manager as well as how to use it, so poke in and read around even if you aren't an i3 user (yet).

The advantages of i3
i3 is peculiar. When you first start opening windows, you will notice that it doesn't do what you would expect from Awesome or dwm. It actually uses all of the screen evenly.
The user can resize windows with their mouse or with the keyboard. They can move windows and change the layout of the workspace. The layouts change the way the windows are presented. There are two ways to use all of the screen real-estate, and two ways to magnify only one window at a time. The ability to have one window open at a time and tab through the rest is surprisingly handy, or at least I thought it was a nice escape from the mess of windows I was making on my desktop.

This provides unparalleled customizability concerning the way that you, the user, arranges the windows. Think of i3 like you would think of GNU screen or tmux, just for X clients. The reason is that each of these "containers" can be further split and assigned splitting rules, so that a user can emulate the one-big-many-smaller paradigm from awesome or dwm.

A mess I made playing with i3

The disadvantages of i3
Disadvantages in i3 are PEBCAK. Everything can be solved in i3 with the well documented protocols and extensive configuration file. However, there are some really hard problems to solve that are not there out of the box. For instance, i3 does not come with a clock (although it does come with a system tray, thankfully!). To combat this I wrote a program that can be evoked that reads the time off (espeak piped into a C++ program using strftime) and put tint2 on an infinite loop (i3 will kill tint2). Are these solutions? No. There should be a textclock widget like in Awesome. But this, like the rest of i3's problems, can be solved with a bit of ingenuity and elbow-grease.

My opinion
As a control-freak, i3 is seemingly a nice tool to take full control of your window manager. There aren't any distractions, the key bindings are sane, the RAM usage is super low (< 5 MiB), and it is oh-so-configurable. It is a lot of effort, but if you are fed up with the glitz and glamour of the mainstream desktop environmnets, come and play on i3's jungle gym.