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.


Saturday, November 10, 2012

Twitter for Learning

    I used to be a twitter hater. It just seemed like some enormous aggregate of worthless quotes from shallow celebrities and humorous meme-like one-liners from role players.

I couldn't have been more wrong...

    It turns out that, sure, although there exists banality on twitter there are also many valuable resources for gaining information regarding just about anything. Here are some practical aspects of twitter that I have discovered:
  1. Twitter can help you recognize trends in software development, by keeping you up to date on the thoughts of the "rockstars" and professionals in the field. Through their comments, and links to recent blogs you can begin to gauge where the field is heading.
  2. If you are learning a new technology, or already have an abundance of knowledge, you can follow tip-providing twitter accounts which give practical, to-the-point insight. For example, I follow @vimtips, @RegexTip, @java_tips, @TexTip, @AlgebraFact and more. Each of these has taught me some new tricks for tools that I use nearly every day.
  3. Finally, twitter can keep you updated on group activities. The last few months I have gone to local Python and Ruby user group meeting which I heard were happening solely because of twitter.
    Through the last few months of using twitter, I'd really like to say that I have gained some practical knowledge. Do you agree and think that twitter is a useful tool for learning? Perhaps I left out more benefits of twitter? Or is twitter just pure evil? Feel free to share your thoughts.

Why I am in Computer Science and Math

    You know, 8 years ago when my dad first tried to teach me programming in BASIC, I couldn't stand it and thought, why in the world would anyone want to do this? A few years gone by, however, I started playing the massive online virtual reality game titled (quite justly) Second Life.

    I spent a good amount of time on weekends in the virtual world, not to socialize, not to participate in the "gun fights" held therein -- something else drew me in -- building. Second Life provides a very rich environment for constructing objects from basic, mutable shapes called prims. I quickly began constructing swords, jetpacks, guns, and anything else that could come to mind. Building these structures was a blast, but these structures were "dumb;" the extent of their interaction with the world was their ability to be attached to my avatar. This led to my discovery of Second Life's scripting capabilities.

    Scripting the structures was like giving them souls (so to speak), engendering in them an ability to interact with their surroundings. My enjoyment for scripting quickly surpassed my enjoyment for building. It was a very fun experience; I would construct objects, give them "minds" or other teenagers on the site would hire me to give their objects minds. In the end I received a new appreciation for programming, through this virtual sandbox. Later in high school, after I had taken a C programming class for fun, I decided that SW development is what I want to do. Ever since, I have been enjoying learning about new technologies, languages and methodologies and I hope to share some of these here.

    "What about math?" you may ask. Well, the story of my experiences with math is simpler: I have been doing it since I was a small child and it has never ceased to be fun, challenging, and exciting for me.