Wednesday, January 29, 2014

My Functional Programming Story

The most honest thing I can say about my dabbling with functional programming is that it was all spawned out of boredom.  Towards the end of last year, I was getting very rapidly bored of the computer science curriculum at my previous school, which I had been following for the preceding two years.  Java is (compared to many) an inherently boring language, and progress in both classes I had taken in it was slow.  I decided that I had had enough of that, and started to search for something more interesting.  A close friend recommended I work through MIT's Structure and Interpretation of Computer Programs, aka SICP.  I started it, and quickly became obsessed.  You see, SICP is a book written for learning Scheme, a LISP dialect and a functional programming language.

The difference was profound.  I was used to specifying algorithms as a sequence of instructions.  I would try to generalize, as that instinct had been pushed thoroughly into my brain earlier on through some means I don't quite know.  But there was always something missing.  Object orientation did lots, but getting globs of code to fit together the right way was still arduous, and I always had to be careful that I didn't mess up some fact of the global state in setting it up.

What's more, there was recursion.  I was an absolute novice in the world of recursion.  Having only seen recursion in a world without first class functions or tail call optimization, it seemed to me to simply be a memory-expensive way to solve problems that probably would be better solved by a well placed loop.  Functional programming was a breath of fresh air.

So I started working in Scheme, and I enjoyed it.  But, in the process of learning Scheme, I found myself reading a lot of blog/forum posts talking about functional programming and this weird other language called "Haskell".  At first I dismissed it.  It reminded me too much of working with Java (static typing seemed arduous and horrible, and it had *gasps* SYNTAX).  When I started to reach a more stale part of SICP (dealing with mutable state), I found myself looking into tutorials for it and thought "eh, might as well give it a try".  After a bit of trouble at the start, I actually found myself enjoying Haskell even more than I had enjoyed Scheme.  Typeclasses and the Hindley-Milner system made static typing less painful, and the difference in amount of testing required pushed it to being a net positive.

So I played around with Learn You a Haskell and a few other sources for practice problems, and before I knew it I was solving some relatively simple problems in Haskell.  But that wasn't all.  I needed a project, something to work on for more than a few afternoons.  I was starting to get interested in compilers, but knew I was nowhere near ready for writing one of those, so I decided an interpreter was the next best thing to make.  And, whaddaya know, writing a Scheme interpreter turned out to be a common way of transitioning out of the beginner stage of Haskell development.  So that was fun.

So I guess this is the part where I compare my experience with Scheme and Haskell as forays into the functional.  I'm certainly glad I started with Scheme.  Sure, it's not as "pure" but the lack of syntax allowed me to think only about the program logic, which was a big enough transition from the kind of reasoning I was used to doing while programming.  I’m not sure I’d have taken to Haskell anywhere near as readily at first.  In addition to that, Scheme has the advantage of being eagerly evaluated.  While I might prefer lazy evaluation now, it would have been yet another thing to understand before I could effectively reason about what was going on.  So I would definitely recommend learning Scheme or some other Lisp first.  That being said, if you want to get good at programming, here’s the most important part: make sure it never stops being fun.

Lastly, I feel this concept must be addressed in any discussion about learning Haskell: Monads.  I don't really understand what all the hullabaloo is about.  They're honestly not that difficult.  That moment when you really *get* monads is a fun one, and really changes the way you look at computing from then on, but it doesn't seem like they really deserve the altogether nasty reputation they've gained.  I remember, when I was but a schemer and not a haskeller as well, thinking that Haskell was actively against being useful.  Why did I think this?  Because everyone I had ever heard writing about monads said they were silly constructs that just made everything more difficult.
Simon Peyton Jones often likes to say that the biggest mistake they made in developing monadic I/O was to use the word “monad”.  Taking terms from category theory leaves lots of people scared that they’ll have to understand difficult math to do anything useful in Haskell.  This isn’t true, and don’t let anyone trick you into thinking it is.  Understanding category theory will enrich your understanding of Haskell’s type system, but it certainly isn’t necessary!

Another thing worth noting is that Haskell is what really forced me to understand package managers and command line tools.  The lack of any serious IDE or similar batteries-included setup had me stuck wandering around trying to find different ways to fix everything as it broke.  I think I went through three different installs of the Haskell Platform.  If you're on OS X, install it with Homebrew.  Seriously.  When you're trying to fix whatever cabal-install broke this time, you'll thank me.  It also forced me into using general purpose text editors like Sublime (my first editor) and later Vim.  If it weren't for working in a language without an IDE, I might never have switched.  Boy, was I missing out.  Learning a general-purpose editor makes it so much easier to just take a language and go, whereas if you felt too bound to your IDE you might spend the first several days trying to find one that you considered satisfactory.

This is a story in progress, as I haven't stopped coding and I don't see myself abandoning functional languages anytime soon.  My most recent endeavor is to start playing around with making a (relatively rudimentary) computer algebra system.  Expect updates on that as I work more on it (and maybe even a bit of literate code!)

Friday, January 24, 2014

The 4 Best Languages To Learn For An Experienced Programmer

In a previous article I wrote about which programming languages to learn first.  Those were the "simple" ones.  Now for the... let's say, advanced, languages.
Important caveat:
These are on my advanced languages post for a reason.  Any IDE's they have tend to be on the weak side, so you should be comfortable working in a text editor.  Vim and Emacs are the classics, or one of those silly graphical editors if you really must (though their support will generally be weaker).  The previous post was not meant for the faint of heart.  That applies triply to this one.

Haskell
Those of you who've talked to me in the past 6 months knew this one was coming.  This wonderful language is known for three things:
1) Elegance - Lazy evaluation and a few bits of sugar make code ridiculously simple looking
Quicksort has never been simpler
The Sieve of Eratosthenes (that operates on infinite lists) in 3 lines
2) The ridiculously rich (though also absurdly rigid) type system.  Typeclasses provide a safe way to do overloading.  The Hindley-Milner type inference system allows you to have the full assurance that static typing grants you without bogging your code down in excessive type declarations.  That being said, while it's possibly the best part of Haskell, it's also often the most infuriating.
3) Being difficult to learn.  Now, I'm not convinced this reputation is deserved.  But, given that monads appear to be the most tutorialized subject in programming, your mileage may vary.  Monads essentially function to encapsulate different bits of programming where the sequencing is important in a lazily evaluated language.  Once it clicks it seems absurdly elegant, but it could take a bit of time.

How to install:
On Debian: sudo apt-get install haskell-platform
On Arch: pacman -S ghc cabal-install haddock happy alex
On Mac OS X: First, install homebrew - if you didn't already have it, you definitely should.  I'm going to write another post about it later.  Then it's as simple as "brew install haskell-platform"
Anything else: Go to this page

IDE/Editor support:
This page should have all the info you need.

Recommended tutorials:
I'd explain my recommendations at length, but someone on Stack Overflow already did it for me

Racket:
Now, I know what you're thinking: "You recommended Racket last time!"  But I only talked about it in an introductory context.  Now, I'm recommending something different:  Read the last few chapters of SICP.  Work through The Seasoned Schemer.  But, whatever you do, learn macros.  When you do, you'll understand all the fuss over Lisp's parentheses.  Their biggest benefit is that their use essentially means that Lisp has no syntax.  And that makes metaprogramming as easy as normal programming in many other languages!  This is ridiculously important.  Macros allowed the Racket developers to graft a static type system on top of the base language, and added async to Javascript.  The first one I have some vague sense of how it was done.  The second one, though, strikes me as black magic.  All the more reason to learn!

How to install:
I'd recommend working from here if you want to use the racket IDE.  But, if you want to use Vim or Emacs there's support for those too.  I'd recommend searching your favorite package manager for racket.
Mac OS X: brew install plt-racket
Debian-based OS':

sudo add-apt-repository ppa:plt/racket
sudo apt-get update
sudo apt-get install racket


Racklog:
The best thing about Racket is that it comes with a bunch of other languages built on the same syntax and editor using macros.  Racklog is a great example (as is Typed Racket, and Lazy Racket).  If that doesn't compel you to learn more about macros I don't know what will.  That being said, the reason to learn Racklog as well is to get used to yet another programming paradigm: logic programming.  I would recommend prolog, but given that it isn't used anywhere, there's no real reason to learn it beyond just getting used to the paradigm, and Racklog will be more than adequate for that.

Logic programming feels even more like magic than functional programming.  You don't specify computations at all.  You specify a few characteristics of the answer you're looking for, and out pops the answer!  I'd swear it was magic if I hadn't spent time looking at the source code for Racklog.

Recommended Learning Resources:
I linked to the official Racklog tutorial above, though you can also learn much of it (with slightly different syntax) in The Reasoned Schemer


APL:
Now, don't get me wrong, I don't expect you to ever use APL for anything.  You probably won't even use too many ideas from it elsewhere.  So why do I say you should learn APL?
Simple.
It's fun.
More specifically, I recommend learning J, a modern implementation of APL that thankfully only uses the ASCII character set.
Now, if you've never heard of APL, I guess it needs some introduction.   APL (A Programming Language) was originally designed by Kenneth Iverson as a system of mathematical notation for array manipulation (the name APL obviously had yet to be dreamt up).  When he was working with IBM in the 60's, he realized that it was also a useful structure for describing program operations.  And thus APL was born.
Array programming is... interesting to say the least.  You can write just about any (purely numerical) computation in one line.  But, APL has also been called the world's first and only write only programming language, and probably rightly so.  Reading old programs is often damn hard, as the syntax provides little to no redundant information.  Write it once, hope it works.  That being said, I'm pretty sure J is turing complete, so it'll still do.

Monday, January 20, 2014

The Best 3 Languages for Learning to Code

It seems like everyone's writing articles about different silly little ways to learn programming.  "Learn to code in 24 hours!", "Become a Software Developer in 30 Minutes", and of course the so-called "Hour of Code".  Now, I may have my gripes about these programs, but they're really great for their target audience, and that is NOT PEOPLE WHO WANT A CAREER IN SOFTWARE.  Maybe they want a basic understanding.  They want to be able to listen to developers talk and understand it at a deeper level than they understand magic.  And that's fine.  That's not this post's target audience.  If you want to learn to be good at coding, these are the languages you should learn, in order:

Javascript
Reasoning:
Along with some really basic HTML/CSS anyway.  Why Javascript, you ask?  Because web programming is simple.  Or, more accurately, because web programming has been optimized to let you do a few simple, introductory things really easily.  When I was learning Javascript, I was building some simple interactivity into a (admittedly sparse / primitive) website in no time.  It's probably easier to get started with Javascript than just about any other language.

Recommended tutorials:
Codeacademy's Javascript Track alongside its Web Track
Also, a good html tutorial if you don't want to use Codeacademy can be found here

Topic Checklist:
Basic I/O and control flow, making coding feel not-scary

Python
Reasoning:
After getting your feet wet with Javascript, I'd recommend learning in Python.  The reason I suggest switching so soon (rather than getting more thoroughly acquainted with Javascript) is that there are simply more (and overall better quality) resources for learning Python.  You have O'Reilly's Think Python, Codeacademy's Python Track, and MIT's OCW Course and their course on EDX (which there are apparently multiple instances of), and many, many more.  Working in Python will help you get used to thinking in the more structured way that is required to code effectively, and will start to teach you some good habits in a relatively natural way.

In addition to that, significant whitespace is a great thing.  Sure, it can create funny situations, but for the most part what it does is enforce clean code.  And trust me, you want to teach novices to write clean code.  I've seen abominations produced by those who weren't instructed in how-to-write-code-that-others-can-read, and I'd prefer not to have any personal responsibility in that.  Granted, it's still possible to write unreadable code in Python, but it takes way more effort.

Recommended tutorials:
I'd recommend all of the tutorials I mentioned above.  They're all really good, just go with whichever makes the most sense for your learning style.

Topic Checklist:
More control flow, 
Loops, 
More involved IO,
Basic Object-Oriented coding,
Thinking in code

Scheme
Reasoning:
Here's the one I might take some heat for.  That being said, whenever you read about what some of the developer-"celebrities" (e.g. most of those interviewed in Peter Seibel's Coders At Work), almost always you find that they worked with Scheme (or some other Lisp dialect) early on in their coding-education, and did a lot of work with it and other functional languages from then on.  The nice thing about functional programs is that they're much easier to reason about.  Some of the tools feel way more powerful (functions as data being the main one about functional programming as a whole, alongside macros in Lisp specifically), but in my mind the chief benefit of relatively pure functional languages is what they *don't* have much of.  Working with mutable state is much more of a hassle than in the previous few languages.  The emphasis on linked lists instead of arrays.  Now, why would I say that working with so much less is helpful?  The reason is simple.  Good code is easily maintainable code is easy to reason about code.  And being restricted to the purely functional tools makes it way easier to figure out exactly what's going on with your program / where it's going wrong.

One important caveat though is that I have a very specific Scheme implementation in mind when I make this recommendation: Racket.  Think of it as a batteries-included version of Scheme.  Most of the tutorials for Scheme, unlike the languages above, don't talk much about the specific way to run it on your computer.  The reason for this is that when you're working in Scheme (or other functional languages) you're not supposed to be thinking too much about the low level details of what the machine is doing.  Instead, you think of the basic algorithmic efficiency, and reason about the program in a more abstract way.  This emphasis on abstraction allows you to create lots of easily reusable code, that can be thrown at all sorts of different problems.

Recommended tutorials:
There's also about as many really good resources for Scheme as there are for Python.  My personal favorites:
The old classic, still probably the best around, but not for the faint of heart: The Structure and Interpretation of Computer Programs
Slightly lighter but still on the tough side: How to Design Programs
Another great resource, for those who found the first two too dense: Realm of Racket
And, on a similar level with Realm of Racket: The Little Schemer

Topic Checklist:
Thinking recursively,
Functions-as-data /  higher order functions,
Data structures,
Big-O efficiency,
Higher order everything,
String processing,
Making reusable code,
Some basic algorithms (sorting algorithms being the classic case)

Conclusion:
There are lots of languages worth learning (or at least getting familiar with), but I'd say these are easily the best three for getting started programming.  That being said, I'm curious, for any programmers reading this, what were your first three languages?  What order did you learn them in and why?

Saturday, January 18, 2014

10 Chrome Extensions to Change the Way You Browse

Browser extensions.  They add a great layer of customizability to an otherwise bland browsing experience, but they come with a cost, namely, memory and cpu time.  I myself am guilty of overextending, and both Chrome and Firefox make it very easy to do this, so I often have to run through my extensions page and disable them all then reenable.  Here are the ones that always make the cut.

First, blockers that'll remove things you didn't want to be there in the first place:

1) AdBlock - this one's a must.  Anyone who uses browser extensions knows to get this one.  Now, there's a bit of confusion over whether to get this or AdBlock Plus.  I personally prefer AdBlock because
a) Lighter memory footprint - it used anywhere from 10-100 mb less memory when I checked Chrome's task manager
b) Better customizability - AdBlock makes it way easier to add your own filters, which is useful if you ever find anything it didn't catch

2) Disconnect - probably the best of the different tracker-blocking addons.  It runs in the background, nabbing as many trackers as it can and stopping them from collecting data about you.  Now, from a privacy standpoint this may not be ideal, but from a speed standpoint it definitely is.  That's a lot of scripts that your browser no longer has to run and load, and a lot of cookies that don't get stored.  And, speaking of not running scripts...

3) ScriptBlock - Sure, it's not quite as nice as Firefox's NoScript, but it does essentially all of the same things, and having a good script blocker is a must for any good hacker.  But why would you want to block scripts anyway?  Well, the biggest reason is speed.  My browser loads pages without scripts up to 90% faster.  Another good reason is quality control.  There is a strong inverse correlation between number of different domains you have to enable scripts for and the quality of the content of the site.  If they require me to enable scripts from more than 5 different sites just to read an article, chances are I don't actually want to read that article.  If nothing else, you also build up some funny bits of knowledge by having to explicitly allow any domains for scripts.  Some sites seem to show up everywhere.  Learning why is sometimes a fun puzzle (I'm looking at you, Amazon!).

4) Web Of Trust - I have yet to find a more extensive website-rating service.  Web Of Trust takes reports form users all around the world about different websites, and collects them in a total rating, as well as some specific ones depending on the site (vendor reliability, for example).  It'll alert you before you accidentally end up in the sketchy parts of the internet (most download sites, for example).

5) StayFocusd - This addon has probably done more for my productivity than all the others combined.  It lets you set time limits for different websites in different timeframes (e.g. "only 10 minutes on Facebook from 9 -19:00 a day").  There are various ways to configure it so it's annoying to change settings, and attempts to guilt you back into work whenever you try to add time on websites you've listed as distracting.  You can also go as far as to completely block certain websites in different times, if even 5 minutes is  too many.

6)  FooTab - Compared to the others here, this one's simple, but still incredibly useful.  FooTab prevents all tabs that aren't in focus from loading for about 20 seconds after launch, so the first page you want to load can start up that much faster.


Now, the things that add to the experience of web browsing rather than just subtracting:

7) OneTab - this has become my tab manager of choice for Chrome -  it puts a button in your navigation bar that will close all the tabs in your current window and store the url's for later recovery.  Think of it as a slightly less powerful (but also lighter on resources) version of Firefox's tab groups

8) Vimium - adds vim-style shortcuts to Chrome.  If you are or want to become a user of vim I can't recommend this addon highly enough.  It adds standard hjkl scrolling, J/K tab navigation, H/L for the back and forward buttons, and a few other really useful ones.  Once you get good at using it, you'll wonder how you ever browsed without it.

9) FastestFox for Chrome - here be automations that you didn't realize you wanted.  It will automatically load the next page of an article / blog you're scrolling through when you get near the bottom of the page, will instantly look up definitions when you highlight a word, instantly reports how popular a link is, improves google results, and much more.

10) ZenMate - makes it incredibly easy to work with a proxy.  It will run all your traffic through any of their locations (US, England, Germany, Switzerland, and Hong Kong) and encrypt it, increasing anonymity and allowing you to access content that's normally blocked in your country. They're in an early stage so offering unlimited bandwidth for anyone who makes an account now, though that may change down the line.  It's not as secure as TOR, but it's one hell of a lot faster, so unless you really need that extra security I'd use this.
DISCLAIMER: I do not support or advocate any way that you use this addon to break the law.  If you're using it to do something illegal and get caught, that's your problem not mine.

Anyway, that's my top 10.  What are yours?

Friday, January 17, 2014

Hack Your Sleep Habits with f.lux

There is a lot of literature on the subject of color temperature and its effects on the human circadian rhythm and melatonin production.  Here's the summary:
"Many are familiar with the "rods and cones" that provide our visual capabilities, but it was only about 15 years ago that retinal ganglion cells containing melanopsin, which are sensitive to a narrow band of blue light in the 460-480nm range, were discovered, and their unique effect on sleep was investigated.
The experimental research suggests that an average person reading on a tablet for a couple hours before bed may find that their sleep is delayed by about an hour."

Another important thing to note is that human circadian rhythms evolved in a world where artificial lighting wasn't a thing except occasionally for a fire, which was a warm light.  Sunset and fire-light were signals that it would soon be time to sleep.  Daylight (which your screen emulates by default) is a cue for staying awake.  Your body does a whole awful lot to halt melatonin production when blasted with something it thinks is sunlight.

So yes, that ominous blue glow of the midnight hacker is actually as negative as it looks.  But, thankfully, there is a way to solve it.  Just get f.lux.  F.lux is a free, cross-platform utility that figures out when sunset is by checking your location and warms the color temperature.  For the first few weeks it'll seem remarkably orange, but after a little while you'll get used to it.  I can't think of a single tweak I recommend more highly to anyone who likes to use their computer late into the night.

Note 1: if you think you don't need to worry about getting enough sleep, think again.  Literally every study disagrees.  Type "sleep deprivation cognitive" or "sleep deprivation performance" into google scholar and you'll find study after study saying that you need to sleep if you want to have the best possible performance at whatever you do.

Note 2: if you have an iOS device, you can't get f.lux without jailbreaking.  That being said, I'll probably post a guide to doing so relatively soon, though it's not as if others haven't already done that.