Wednesday, December 19, 2012

Maybe, Just and Nothing (More Bragging on Haskell)

Man, this Haskell thing is pretty cool. One of (in my opinion) the most annoying things that a programming paradigm can do is consider unexpected behavior to be normal behavior. For instance, most list searches return -1 when the element you are looking for does not exist. Does returning -1 makes sense? No... Not really. Well, anyways, Haskell defeats this type of thing with the following line (this comes with the standard library):

data Maybe a = Nothing | Just a  

Basically all it means is, there's this data type called Maybe (remember, uppercase means constant) and when it is given a type it may just return it or it may return nothing!

This tool is one of the coolest things I have seen in Haskell, thus far. What is the power in this? You can make functions with expected behavior! For instance, consider this quick, binary search I built:

binarySearch :: Ord a => [a] -> a -> Int -> Int -> Maybe Int

binarySearch l e beg last
 | lookat == e = Just i
 | i == (length l)-1 || i == 0 = Nothing
 | lookat < e = (binarySearch l e i last)
 | lookat > e = (binarySearch l e beg i)
 where i = quot (beg+last) 2
       lookat = l !! i

The Maybe, Just and Nothing allow the function to be loose enough that the function can respond with ,"I couldn't find what you were looking for; I found nothing!" This seems to be a huge plus of Haskell. I don't know of many strongly-typed languages that allow this level of Dynamicity.

Tuesday, December 18, 2012

Latest Project: Lyric Miner

    Towards the end of this past semester, I decided to build a "data miner" in Python, that would contain tools for gathering information from the web. Though I don't have a set direction for where I would like it to go, I did build the first tool: a lyric miner called "LyricThief." My only goal for LT was to simplify the process of Googling lyrics and all that that entails. So, here's what it looks like (please forgive my Linux desktop's matrix-esque windowing):



    As you can see, all it needs is the artist and the song title then viola! It will search a list of sources and bring up the lyrics in this screen:


    From here you can read, edit or save lyrics to a text file.

    As it stands right now, adding sources (or removing them) is easy. All they require is a method to generate what the URL looks like for the site (most lyric sites have a predictable URL format) and a method to parse the URL (I usually do this in 2-4 lines of obfuscated Python code, I know, it's bad). Then, once the source is complete, they are added to the builder and it takes care of the rest.

    Although I haven't tested it in Windows, it should work if Python 2.7 and  PyGTK are installed. In Linux, however, these things are included with most modern distros by default.

All code is on my github.

Tuesday, December 11, 2012

Functional Fibonaccis in Haskell

Lately, I've been learning my first functional programming language: Haskell. Thus far, it is truly awesome! Look at the following Fibonacci function (I know it's a dead-horse example):

nthFib :: (Integral a) => a -> a
nthFib 1 = 1
nthFib 2 = 1
nthFib n = nthFib (n-1) + nthFib (n-2) 


So, "What makes this simple Fibonacci example so awesome?" you may ask. Well, there are a few things I really like about it, but the main thing is that it actually looks like the mathematical definition for the Fibonacci Series! Also, that first line looks a lot like "nthFib: Z -> Z", you know, that good-old-mathy notation meaning, "nthFib is a function that maps an integer to an integer" (I know, I know, the first 'a' should be a natural number, I'm still getting there). But this is wicked stuff!


Saturday, December 1, 2012

Programming Challenge Sites

    You know, as a developing programmer one of my biggest challenges is finding ways to exercise new languages/skills/tools that I am learning. There's nothing worse than reading about some new language or paradigm and then thinking, okay, I get it but what now? This is where programming challenge sites come in handy! They tend to provide small problems that can serve as an outlet for new skills and technologies as well as stretch your problem-solving abilities. Here are a few of my favorites:


 
1. Project Euler is a superior collection of mathematical programming challenges which grows progressively more difficult with each new problem. At this point there are 267248 members who have solved at least on problem on PE; however, some problems have as few as 100 solvers! PE is by far one of my favorite sites to turn to for programming challenges on my days off; also it's named after one of the mathematical greats, Leonhard Euler.


    2.  Usually, the StackExchange code golf site's member-provided problems are concerned with shortest code-length solutions (thus the "golf") but there are also a frequent slew of bizarre problems. This is such a cool site. IMHO, the feedback-oriented, well designed StackExchange engine works well in code golf form.



3. Rosalind is a biology programming challenge collection. It has a similar feel to Euler, except what it lacks in mathematical problems it makes up for in string manipulation. The challenges are fun, and more suited for beginners in programming (don't get me wrong, there are some really difficult ones!)



4. Dave Thomas' code katas are designed to challenge and insight the mind. Thomas, co-author of Pragmatic Programmer (that legendary book that us developers should all read) constructed a nice little set of though-provoking challenges, that will (if used properly) increase your critical thinking capacities.



5. Cyber-dojo is a set of team-oriented problems with a focus on TDD. It is meant to be a simplistic environment in which programmers are encouraged to focus on the solving; not the solution. It can be a great way to improve problem solving abilities, hone testing skills and expand your knowledge of new (or old) languages.