Lisp after Python after Lisp.

Standard

(Or, Python vs Clojure rant)

This post assumes some familiarity with Lisp and Python from the reader.

Like loads of programmers, I have read most of Paul Graham’s essays, and I always finish them with that feeling that if I’m not using Lisp then I must be an idiot. Like most programmers, I certainly don’t want to be an idiot, so I went ahead and learned Lisp, Common Lisp.

Continue reading

Lack of CS interest in Google trends.

Standard

So the other day I was checking out Google trends, and I figured I’d check how my favorite editor, Emacs, was doing.

Turns out that there has been a steady decline in the number of google searches done for “emacs” from 2004 to Jan 2009.
I wanted to see how “eclipse” was doing, but I figured that most of the people that would search for eclipse must mean an actual eclipse, not the IDE. So I then proceeded to see how Java was doing:


As you can see, there is a very similar decline, though with less of a slope.

Intrigued, I checked the trend for the much more general term “Algorithm”:


Almost the same decline as “emacs”!! Except for spikes that seem to correlate to school terms.

Now take a look at the trend for “computer science”:


If you ask me, I would tell you that this is a sign of a steady decline of interest in computer science/programming, since the very general term “math” doesn’t show the same slope:
(Note that there are very marked valleys correlating with school vacations).

What’s the angle between two continuous functions?

Standard

I want to write about something very cool I learned about in my Linear Algebra class the other day.
Since since the sum of two continuous functions is continuous, and a continuous function multiplied by a scalar is continuous, the set of all continuous functions defined on the domain [a,b] is a vector space.
You can define an inner product [F|G] as the integral of the product F*G from point a to b (This operation has everything it takes to be an inner product). The inner product naturally gives us a norm. ||F|| = sqrt([F|F]), and an angle between functions: [F|G] = cos(theta) * ||F||||G||. The norm gives us a distance ||F-G||.
So now we have distance between functions and angles between functions. That means we can project a function onto another the same way that we can project a vector onto another in R^n. That’s awesome! And potentially useful. =P

(Edit: Yes, it’s useful. You can deduce the Fourier Series by noting that the cosine and sine as basis for this vector space)

Word Challenge cheating program

Standard

I have been playing Word Challenge on facebook, it is a very fun game. It made me start playing with anagrams, and I decided to write an anagram-solving program. However, I read about Donald Knuth’s insight that if you sort a word and its anagram alphabetically, it will give the same word.
Example:
sergio -> egiors
orgies -> egiors
So, by using a hash table it is pretty easy to find anagrams in constant time. You just have to associate a word made up of ordered letters to a list of all the possible anagrams. You can do it in O(n) by iterating through a dictionary file. There’s no problem to solve when there is already an optimal solution.
I then decided to write a program that finds every possible word that can be formed by a group of letters (Which would be perfect to use if I ever wanted to cheat on Word Challenge. Which of course I never would! =P) .

Continue reading

Interfacing Arduino with C++ and libSerial.

Standard

I know that there is a section at the Arduino Playground on interfacing the Arduino with C. However, I found it is much easier, almost trivial, to use libSerial with C++.

(libSerial works on POSIX systems. I guess Windows users could use it through Cygwin but I’m not sure)
(Update: No, they can’t)

First, you import your libraries and define the port you will be using:


#include &lt SerialStream.h &gt
#include &lt iostream &gt
#define PORT "/dev/ttyUSB0" //This is system-specific

SerialStream ardu;

using namespace std;
using namespace LibSerial;

The above code is assuming that the arduino is bound to /dev/ttyUSB0. Make sure to point the stream to the real device on you computer ;)

A new serial stream "ardu" is created.

Now let's define a simple function to setup the Arduino for communication.

void open(){
    /*The arduino must be setup to use the same baud rate*/ 
    ardu.Open(PORT);    
    ardu.SetBaudRate(SerialStreamBuf::BAUD_9600);  
    ardu.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
    (....)

At least in my case, this simple function is enough to have a working serial communication with the microcontroller.

Now I'll write another simple function that sends one byte through the serial port to the arduino and then reads a string, which gets interpreted as an integer..

int get(char out){    
    int res;
    char str[SOME_BIG_SIZE];
    ardu << out;
    ardu >> str;
    sscanf(str,"%d",&res);
    return res;
}


See? It's easy! :)
If for some reason you must use standard POSIX libraries, here is a great article on POSIX serial programming.

If you have any doubts, post a comment.

Rotating cubes.

Standard

A couple of days ago I got 3 LIS3LV02DQ accelerometers from Sparkfun electronics.
Today I finished a working demo showing a cube that rotates as you rotate the device.
This accelerometer has some good documentation to learn how to use it. There is also a post in some blog with a specific example for the arduino.
That example didn’t work for me, but when I wasn’t sure about something, it did help to take a peek at it. Reading the datasheet carefully is extremely important.
The arduino has built-in SPI support, which helps a lot since the accelerometer uses SPI (or I^2C) to communicate. However, it is bound to 4 specific pins in the board, and if I’m going to use the 3 accelerometers at the same time without any additional hardware, I am going to have to write a software implementation, since I would be using 12 pins.
The interface to the arduino was written in C++. It uses libSerial, which makes communicating with the arduino almost as trivial as when using Python.