Sleeping Cyborg

Jonathan David Page talks about whatever he happens to be thinking about. Sometimes other people join in.

Email · @parathetic (Twitter) · @jdpage (Github)
Subscribe to feed


A collection of cool people and projects.

A Simple(ish) Explanation of Haskell Function Signature Madness

by on 22 August 2012
in , ,
with some comments, maybe.

A clarification regarding my use of the word "simple(ish)": I am assuming, firstly, that you are already comfortable with programming in an imperative language such as C, Java, or Python, and secondly, that you already know at least a little Haskell.

Haskell type signatures, to the uninitiated, are a little odd. Take the following simple two-argument function:

plus :: Int -> Int -> Int
plus a b = a + b

If you're coming from an imperative language, you might be tempted to read that signature as "this function takes an Int and an Int and returns an Int". That'll do in most cases, but it isn't really true. And the fact that it isn't true is a really cool feature of Haskell.

The little -> arrow is a right-associative operator. So you whould read Int -> Int -> Int as Int -> (Int -> Int), which doesn't help anything at all, because now it looks even worse. And now here is the kicker: all functions in Haskell take exactly one argument. One. Even our plus function there. Which is odd, because it sure looks like it takes two arguments. The thing is, Haskell does a little magic trick for us (which isn't really magic). This magic is called currying.

The trick is that when you do plus 2 3, two things happen. First, 2 is applied to plus. Application is a fancy way of saying that an argument is given to a function. This application results in a new function, which also takes one argument. 3 is applied to that function, which one might notate as (plus 2), and returns an Int, 5.

In short, plus 2 3 is the same as (plus 2) 3. So the Int -> (Int -> Int) means that plus is a function which takes an Int, returning a function which takes an Int, returning an Int.

So why is that useful? Well, consider the builtin function map, which has the signature:

map :: (a -> b) -> [a] -> [b]

Basically, it takes a function which takes a thing of type a and returns a thing of type b, and an array of things of type a. It then spits out an array of things of type b, which is generated by applying the function to every argument the array of things of type a.

So map (\b -> plus 2 b) [3 1 4 1 6] returns [5 3 6 3 8].

Now, remember that (plus 2) returns a function, right? So you could also do:

map (plus 2) [3 1 4 1 6]

and get the same result.

tl;dr: Calling a Haskell function with not enough arguments basically returns what you might think of as a half-called function. It's got some arguments already, you just need to supply the remaining ones. And it's just another function.

bearjcc asked: What number do you get when you call the Ackermann function with Graham’s number as arguments?

by on 28 April 2011
in ,
with some comments, maybe.

The Ackermann function is a mathematical function that returns really really large numbers. For example, \(A(4,2)\) is an integer with over 19,700 decimal digits.

Graham's number is a holy-moly massively horrifyingly huge number which makes a googolplex look like diddly squit. Actually, it makes numbers which make numbers which make googolplex look like diddly squit look like ant droppings look vanishingly small. It it unimaginably huge. I do not have words to explain how much massively huger than pretty much any number you can think of it is.

As you can imagine, \(A(g_{64}, g_{64})\) is quite a large number.

PS-- I'll answer the \(e^{\pi i}\) question soon, I want to type out a proper explanation for that.

What is light?

by on 26 April 2011
in ,
with some comments, maybe.

This is not very specific, so I will give a general explanation of the word "light" to make sure I've answered your question properly.

Light, as in visible light, is a certain small band in the electromagnetic radiation spectrum. Electromagnetic radiation is a pair of perpendicular magnetic and electric fields travelling in tandem through space. Due to quantum weirdness, it is equally well described as a particle (called a photon) travelling through space. This has little to do with cats, which seems to be what comes to mind when most people hear the words "quantum weirdness". Resist such foolish thoughts.

Light, as in the visual sensation of light, is caused by chemical reactions in your retinas caused by electromagnetic radiation in the visible spectrum hitting them. Certain frequencies set off reactions in different cells by different amounts ("rod" cells respond purely to brightness, while "cone" cells respond largely to the chroma). For something that produces images as pretty as it does, this is a rather unromantic description, and if you feel that it has removed joy from your life, you should have skipped it. Don't say I didn't warn you because we all know I didn't already.

Light can also be considered in a metaphorical context, where it is often associated with knowledge, morality, reason, and other positive concepts. Basically it's associated with "the good guys", which is weird because everyone knows that black is the coolest colour.

Light can also be used as an adjective to describe a colour that is paler than usual, or to describe something with comparatively little mass. I say comparatively, because it's quite subjective and what is laughably light to me might very well be quite heavy to you.

I think that about covers it.

Ask me anything. Maybe I'll answer.