Friday, December 26, 2014

Think like a Fundamentalist, Code like a Hacker...


I've just finished Eric Meijer's Intro to Functional Programming on edX. Since the course was taught in Haskell, I also managed to get my feet wet with the language and now feel prepared to work through Learn You a Haskell and Real World Haskell.

One of the course's mantras has been "think like a fundamentalist, code like a hacker". The idea is to start with an approach that is fundamentally correct and iteratively refine the implementation. This is a pretty convincing philosophy for Haskell (and perhaps functional programming in general), since things like type checking and pattern matching facilitate reasoning about the correctness of programs. An interesting question that follows, however, is how to balance readability/clarity vs the efficiency of solutions that are fundamentally sound but "hacked".

There's a good example in Graham Hutton's Programming in Haskell called "Making Append Vanish" (chapter 13, section 6). We start with an example of a recursive function that reverses the elements of a list:

reverse              :: [a] -> [a]
reverse []            = []
reverse (x:xs)        = reverse xs ++ [x]


Because this solution using the ++ append operator is relatively inefficient (requiring quadratic time), Hutton demonstrates using induction that a linear time solution can be derived:

reverse              :: [a] -> [a]
reverse xs            = reverse' xs []


reverse'             :: [a] -> [a] -> [a]
reverse' [] ys        = ys
reverse' (x:xs) ys    = reverse' xs (x:ys)

At least for me, the second solution takes much longer to digest, and hence is less clear -- but it is demonstrably correct and far more efficient. I'm not sure if it's really fair to call the second solution "hacked". But it does illustrate an interesting aspect of the functional programming paradigm, which I'm looking forward to exploring further.

Wednesday, December 24, 2014

Intro to Git and GitHub


While working through the last two weeks of the Functional Programming course on edX, I took a break to run through the short How to Use Git and GitHub class on Udacity.

I've been using Git for a while, but this was a nice introduction to GitHub -- the course is very well-designed and felt like a good balance of fundamental principles and practical application.

It also pointed me to these two very useful environment tweaks:

git-completion.bash
git-prompt.sh

Tuesday, November 11, 2014

Hiatus and Haskell


I've been on hiatus as I started a new job this year... but I have resumed some coursework and hope to document progress on a more regular basis.

Erik Meijer's Introduction to Functional Programming on EdX has been an excellent (and gentle) introduction to the subject, and has also served as a good way to dive into Haskell. I suspect Learn You a Haskell will be much easier to digest after this course.


Monday, February 3, 2014

Project Euler 51 - 60


Project Euler Solutions to Questions 51 - 55
Project Euler Solutions to Questions 56 - 60

With some of the problems, I'm starting to run up against the one-minute execution time guideline.

From a computational perspective, I feel as if I'm still reasonably well equipped to handle the problems. Various parts of my Coursera and Udacity education have come in handy, including dynamic programming, basic cryptography, and the general approach to breaking down problems as presented in Peter Norvig's class.

From a number theoretical perspective, though, I'm running into some trouble.  In Problem 60, for example, the naive brute-force approach runs in O(n^5), where n is on the order of 10,000 (and possibly much larger). There are surely ways to cut down the search space, but I need to do some more reading on primes.

On the subject of primes, I need to find a better primality test.  So far I've been able to get by with a combined approach of:
  • generating all primes that are less than 10,000,000 via the Sieve of Erastothenes
  • checking primality of larger numbers by prime factoring and counting the number of factors
Maybe the next step is to implement a faster sieve or something like the Miller-Rabin test.

Saturday, February 1, 2014

Project Euler 36 - 50


Project Euler Solutions to Questions 36 - 40
Project Euler Solutions to Questions 41 - 45
Project Euler Solutions to Questions 46 - 50


Problem 46 is about one of Goldbachs' conjectures that turns out to be false.

The original Goldbach conjecture is an interesting problem.  Following Wikipedia's example, I tried creating a graph that shows the number of ways to represent even integers as the sum of two primes (the number of "Goldbach partitions").  From the small dataset, it looks like the number of Goldbach partitions increases with integer size, and moreover there is some periodicity to the number of partitions. But of course, such observations are meaningless in the context of mathematical proofs.



Monday, January 27, 2014

Project Euler 26 - 35


Project Euler Solutions to Questions 26 - 30
Project Euler Solutions to Questions 31 -35


I find myself using more functional style components these days, e.g. recursion, list comprehension, and functions with single-line returns to check multi-clause conditions.  There's an elegance to the simple, piece-meal constructs.  My next big project is to learn Haskell...