Two arrows means two parameters. In the second case you're passing in a first parameter, which creates a function waiting for it's second and final parameter. In the first case you're still waiting for both parameters.

Arrow can be seen as 'returns a value'. So 'int -> int -> int' should be read as a function that takes and int and returns a function that takes an int and returns an int.

The effect is more formally know as currying, so you may want to read up on that.

Hope that makes sense!

Robert

By on 4/26/2010 2:29 AM ()

The function type is written without parentheses because you can interpret it as

(int->int)->int

i.e. a function of 2 ints returning an int, or

int->(int->int),

i.e. a funtion of an int, that returns a partial function from an int to an int (as in (+) 1 ).

By on 4/27/2010 4:16 AM ()

@mau,

actually the arrow in function types definitions is right associative so int -> int -> int precisely translates to your second interpretation i.e. int->(int->int). The first one, (int->int)->int specifies an entirely different type, that is a function taking a function from int to int and returning an int (for instance let g f = 1 + f 1).

edit : sorry for the redundant answer (one should obviously always check for a brian's posting just before pressing the 'post' button [:)])

By on 4/27/2010 4:54 AM ()

The function type is written without parentheses because you can interpret it as (int->int)->int i.e. a function of 2 ints returning an int, or int->(int->int), i.e. a funtion of an int, that returns a partial function from an int to an int (as in (+) 1 ).

Conceptually this gives you an idea, but being pedantic, the parentheses matter, and

(int->int)->int

is a different entity, specifically a one argument function that takes an int-to-int function as an argument and returns an integer as a result. This is different from a

int->int->int ( == int->(int->int) )

which takes an int and returns an int-to-int function.

See also

[link:lorgonblog.spaces.live.com]

By on 4/27/2010 4:45 AM ()

Err, Brian's right of course. I was just trying to make it easier to interpret the signature :-)

By on 4/27/2010 5:26 AM ()

Thanks very much guys.
That explanation was better than I hoped for.

By on 4/27/2010 4:13 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper