News for hackers. Stuff that matters.
Re:The Day Python Embarassed Imperative Programming by tripzilch on Monday February 27, 2012 @08:27AM (#3638859)

    m a -> (a -> m b) -> m b
Okay, so what does this mean? I thought it was `f x -> x * x` is a function definition, right? Or am I completely mistaken here? Because in the above code, what is the function name, the parameter and the return value? I know "everything is a function" in Haskell but I really can't see through the multiple arrows here. Any help?
Re:The Day Python Embarassed Imperative Programming by haberman on Monday February 27, 2012 @11:08AM (#3639414)
This is one of those things that functional-programming people seem to "get" without ever making the explanation very clear.

First of all, this is a function declaration (ie. prototype), not a definition. So this is describing a function type, not a function implementation. But that doesn't help very much because it's still not very obvious what the function type means.

The shortcut way to understanding Haskell function declarations is this; if you see:

  a -> b -> c -> d
in your head, think of it as:

  f(a, b, c) -> d
In other words, it is a function that takes three parameters of types a, b, and c and returns type d. Everything before the final -> is a parameter and the final type is a return type.

My "shortcut" isn't literally true, obviously. Here is the gory detail.

In Haskell, every function takes at most one parameter. Functions of multiple parameters do not exist; they are simulated through a technique called "currying." When you think you're calling a function of more than one parameter, you're actually calling a series of functions, each of which takes exactly one parameter. So in Haskell, if you call:

  f a b c
This is actually parsed as

  ((f a) b) c
Or in more C-like notation:

  f(a)(b)(c)
In other words, you call a function with a single parameter "a", which returns a function that you call with a single parameter "b", which returns a function that you call with a single parameter "c."

Likewise, the Haskell type declaration:

  a -> b -> c -> d
Is actually right-associative, so it's parsed as:

  a -> (b -> (c -> d)))
Which is why the whole thing works.

So to parse:

  m a -> (a -> m b) -> m b
Think of it as a function that would be called like so:

  f(m a, g) -> m b
Where g is a function that would be called like so:

  g(a) -> m b
The "m a" and "m b" business you can think of as being a lot like M<a> and M<b> in C++.
Re:The Day Python Embarassed Imperative Programming by tripzilch on Tuesday February 28, 2012 @07:14PM (#3645749)
Thank you! It's coming back to me now :) This was taught to me in college, I know about currying, the right-associativity was the big part I missed!
Re:The Day Python Embarassed Imperative Programming by danieldk on Monday February 27, 2012 @08:45AM (#3638924)
It's actually more insightful to see the full type signature:

  (>>=) :: Monad m => m a -> (a -> m b) -> m b
So, >>= is a function that takes:

- A value a in a monadic type m

- A function (a -> m b): a function that takes a value a and returns a value b wrapped in the monadic type m.

And it returns:

- A value b wrapped in the monadic type m.

It helps to look at the definition of (>>=) for a particular monad. The article mostly discusses computations that can fail (None vs an object in Python). The corresponding Haskell monads are Maybe and Either. The signature for (>>=) in the Maybe monad is:

    (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
What the grandparent points out is that a monad defines a structure (how to combine expressions that result in wrapped values), but not semantics. The semantics are defined in the definition of a particular monad. E.g. the Maybe monad models failure in computation, while a monad such as MonadRandom does something different altogether (providing random numbers, while threading the state of the random number generator).
Re:The Day Python Embarassed Imperative Programming by tmhedberg on Monday February 27, 2012 @08:36AM (#3638892)
Sorry for not being clear. That's just the (abbreviated) type signature of >>=, which is formally written as

    (>>=) :: Monad m => m a -> (a -> m b) -> m b
It's not the definition of >>=, though. The definition is left up to the specific Monad instance that's defining it (which is basically the larger point that I was trying to make).

In English, that type signature basically says that >>= is a function which takes a monad and a function that operates on the monad's "contents" as its arguments, and returns the result of applying the function to the monad's contents. Any data type for which that abstract structure makes sense (and that also complies with the basic "monad laws") can be made an instance of Monad and provide its own definition for how >>= operates. That definition can be wildly different for different data types; the specific definition that the article focuses on is the one for the Maybe type.

Re:The Day Python Embarassed Imperative Programming by masklinn on Monday February 27, 2012 @09:04AM (#3638970)
> Okay, so what does this mean? I thought it was `f x -> x * x` is a function definition, right? Or am I completely mistaken here?

In this case, it's a type signature for a curried function. So

    a -> m b
is a function taking a value of type `a` and returning a value of type `m b`. In C# you would write:

    Function<A, M<B>>
so `m a -> (a -> m b) -> m b` is a function taking a value of type `m a` and a function with signature `a -> m b` and returning a function of type `m b`. Translating to C# again,

    Function< M<A>, Function<A, M<B>>, M<B> >
> Or am I completely mistaken here?

Pretty much yeah. In haskell, a function is either a top-level binding:

    someFunction arg = doSomething arg
or an anonymous function which is introduced by the character `\` (because `\` looks like `λ`, kind-of):

    \arg -> doSomething arg
In the expression you quoted, there is no function name, these are all types. The function name in this case is `(>>=)`
Re:The Day Python Embarassed Imperative Programming by yogsototh on Monday February 27, 2012 @08:39AM (#3638902)
m a: means a type which take a parameter (another type).

For example,

data Maybe a = Nothing | Just a

    - The m is for "Maybe"
    - The a is for "a"
For example:

    (>>=) (Just 3) (\ x -> Just (show x))
should returns: Just "3".

    (>>=) Nothing (\x -> Just (show x))
better written

    Nothing >>= (\x -> Just (show x))
should returns: Nothing
Re:The Day Python Embarassed Imperative Programming by jamiltron on Monday February 27, 2012 @12:25PM (#3639697)
Not to be nit-picky, but everything is Haskell is not always a function:

http://conal.net/blog/posts/everything-is-a-function-in-hask...