Haskell

Two years ago, I would have chided myself.

“You choose to write about programming for fun? The thing that’s sucking the life out of the liberal arts, and churning out soulless silicon valley stockbrokers?”

Well, kind of. Back then, I only took computer science because of the high starting salary out of college.  Programming was something like plumbing: you do it for work, not for fun (I’m sure there are plumbers who happily plumb (plumb?) 12 hours a day like they’re smoking pipes instead of manipulating them, but those are few and far between). Of course, plumbing, as well as programming, has elements of complex problem solving, getting from input a A to output B through altering and manipulating information. That takes a fair amount of effort and skill and logic. That was kind of interesting.

But syntax……………Syntax… really was not my thing. You know how code is basically a set of instructions for a computer, (do this, then that, then this), because the computer doesn’t know whenn the word I misspelled actually means “when,” it’s just a careless error, you have to be really careful how your code is typed. For a basic example, if number == 0 works, but if number = 0 doesn’t, because = and == mean different things. This seems like a reasonable fix, but imagine if it’s something more ambiguous, like checking if all the brackets in ([({{([(?)])}})]) close. One tiny ridiculous thing will trip it all up, like a publisher turns down your novel because you had a comma splice on page 247.

Somehow, this struck me as much larger and more abstract than it should have been, like all of human ingenuity and creativity was being shoved into this box for machines, and that’s all the future held: not having or grasping incredible ideas, but adapting them until they could be understood. And that kinda sucked. “Focus on ideas, not reducing those ideas.” I laugh about it now, because I was declaring a war on everything succinct and useful, like ideas were better in the inarticulate un-understandable raw. I’d offer a more fleshed-out rebuttal  if I thought it would be interesting and on topic, but I don’t.

Instead, I’ll offer a comparison to the humanities. You know how you can do a bunch of crazy stuff with literary theory? Approach literally anything from a marxist, feminist, post-colonial perspective? Or you could go historical, looking at long-term trends in thought and events. Like, the humanities is this toolbox that you can dismantle anything and everything with, no limits on abstraction or application of concepts, no absolutes, and that’s what was so cool: intellectually, you could do anything.

Ok, now imagine all that, but instead of the entire intellectual realm, you get everything tangible. That’s computer science.

With technology’s progress, everything will eventually be translatable into a form of manipulable data. So being able to wield that data and make it do whatever you want, means you can do whatever you want (it’s not at the point where it can manipulate intellectual data outside of abstract logic, nor will it get there for a while. But if/once it does, o boy).

Now Haskell.

Put simply, Haskell is just really really really succinct.
I’ll give an example:

primes = filterPrime [2..]

where filterPrime (p:xs) =
p : filterPrime [x | x <- xs, x `mod` p /= 0]

I’m gonna approach this in 3 chunks, because it can be hard to conceptualize all at once.

Part of the code takes in a prime number, then goes through the number line, removing every multiple of that number. If it’s given 2, it would remove all the even numbers. If given 3, it would remove multiples of 3, and so on. That’s what [x | x <- xs, x `mod` p /= 0] does. x `​mod` pmeans dividing x by p, and taking the remainder (ie. 5 `m​od`2 = 1 because 5/2 = 2 remainder 1), where p is the prime number, and x is the number being checked.
The 2nd part (primes = filterPrime [2..] ; p : filterPrime [1st part]) takes the first part, and applies it to 2, then takes the new list with no even numbers, and takes out all the 3s, etc. until you have all the primes.

Actually, this method of filtering primes isn’t new. It’s another thing you can trace back to those arrogant Greek eggheads, this time: Eratosthenes. He thought: “if you remove everything divisible by a certain number, and do that for all numbers, you’ll only have what’s left.” Then someone had that thought today, just in Haskell.

But it’s the way it’s implemented that’s special for me. Object oriented programming might entail something like this:

for i in (1..infinity):
for p in (list of primes):
if (i mod p != 0):
(list of primes).append(i)

You’d conceptualize that as a series of steps.

Step1 : ​​​(increase i by one then do everything below me)
Step2 : ​​​       (go to the next value of p, the do everything below me)
Step3 : ​​​             (check if i mod p doesn't = 0)
Step4 : ​​​                      (if it don't, then toss i on the end)
Step5 : ​​​                        (back to the top)

And that’s ok I guess. But you don’t really need any steps: every single thing we do can be seen as a function. Step1, ie. function1 changes the value of i. function2 changes which prime number is evaluated.
It’s a subtle distinction: “increase i by 1” vs “this function increases i by 1”. One of them seems really restrictive, mainly the 1st. It’s a single step in a larger function. Whereas, the 2nd feels like it’s it’s own function, like it can be re contextualized or manipulated further, because it’s not some cog in a larger function: it’s a function itself.
The 2nd mindset works on different levels of abstraction much, much smoother.

The result is that haskell, and by extension functional programming, is much more conceptual. Functions are denser, to the point where they’re best interpreted not by a series of steps but as ideas. And to come full circle, this addresses one of the major problems I had with programming initially: it was too much like following a series of steps. Of course, it means you have to think about programs more conceptually, and this makes the jump between object oriented and functional programming difficult. But once you’re there……
o boy.

(and I just want to say: I am no expert in functional programming, nor am I even qualified to talk about advanced things like Monads or Functors. The idea of functional programming however, is something I’ve spent a lot of time on, mostly because it’s really really really really cool).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s