Wednesday, October 28, 2009


If you've ever seen the above trying to do a println with an array, I have the solution for you!


My Favorite Join Function

My favorite implementation of a join function (in Java):

public static <T> String join(T[] array, String delimiter)
if (array.length == 0)
return "";
StringBuilder builder = new StringBuilder();
for (int i = 1; i < array.length; i++)
return builder.toString();

Wednesday, October 7, 2009

How Haskell Works

The most baffling programming language I ever learned was Haskell. It is famed as one of the only purely functional programming languages in use today. A hiring rep from Microsoft told me that it was his favorite language, which sparked my interest.

Basic Syntax

You can define a function called "divide" that takes two arguments, divides them, and returns the result.

divide a b = a / b

You invoke the function like this:

two_thirds = divide 2 3

No parens, no commas. Just spaces.

Furthermore, since this is a functional language, you can define functions by simply assigning them from other existing functions.

div = divide
two_thirds = div 2 3

This shouldn't be anything too profound since Python, even JavaScript, can do this.

Now let's make a function that inverts a number:

invert a = divide 1 a
one_half = invert 2

The Confusing Part

Unlike in Python (or any other language I've seen), you can define a function that supplies only some of the arguments to another function.

invert = divide 1
one_half = invert 2

But I thought "divide" took 2 arguments, you might say. It actually doesn't.

How Haskell Works

Every function in Haskell takes exactly 1 argument. To explain this, I'll use Python. Here's the Python equivalent of our divide function from above:

def divide(a):
return lambda b: a / b

Here's how to call our python divide function:

two_thirds = divide(2)(3)

The result of divide(2) is a function that takes 1 argument called b. When it gets b it divides 2/b and returns that. All functions in Haskell behave this way.

Some Examples

negate a = -a
divide a b = a / b

-- here's a list of numbers:
numbers = [1,2,5,10]

-- the map function takes a function and
-- applies it to each item of a list
negated_numbers = map negate numbers
-- produces: [-1,-2,-5,-10]

-- here's our invert function
invert = divide 1
inverted_numbers = map invert numbers
-- produces: [1, 0.5, 0.2, 0.1]
-- equivalently:
inverted_numbers = map (divide 1) numbers

-- more complex:
-- this function inverts a list of numbers
invert_list = map invert

inverted_numbers = invert_list numbers

Obviously, there's a lot more to Haskell than presented here, but hopefully this helps explain some of the semantics of Haskell I had trouble with when I learned it.

Tuesday, October 6, 2009

It's all C's fault: Part II

In a previous post, I vented about the ambiguity of expressions such as (a)-b. Are we subtracting (a) from b, or are we casting -b to type a? I have news: the Java compiler struggles with this same problem.

Integer i1 = (int)3; // valid
Integer i2 = (int)-3; // valid
Integer i3 = (int)-(3); // valid
Integer i4 = (Integer)3; // valid
Integer i5 = (Integer)-3; // compile error
Integer i6 = (Integer)-(3); // compile error
Integer i7 = (Integer)(-3); // valid

There's some automatic boxing of primitive types into wrapper types going on here, a feature of Java 5. The fact that the compiler can handle 3 but not -3 in any particular location is to me a fault. This is with jdk1.5.0_16.

At least I've got evidence that the (Type)expression casting syntax is troublesome.

Sunday, October 4, 2009

Operators VB vs Java

What's the output from these two functions in VB and Java respectively:

Public Sub PrintThings()
Console.WriteLine("3" + "6")
Console.WriteLine(3 & 6)
End Sub

public void printThings() {
System.out.println("3" + "6");
System.out.println(3 & 6);

Friday, October 2, 2009

Name That Function

let a, b, c be Non-Negative Integers
let f(x, y) be a function such that:
f(a, b) = c
f(b, a) = c
f(a, c) = b

what is f?

I'm not going to mess around with the formal notation of math, because the answer isn't something you can't figure out.

How to check your answer: pick arbitrary values for a and b. Use what you think f is and the formula f(a, b) = c to calculate c. Then see if the other two formulas are true with those values of a, b, and c.