Jul 8, 2011

f# essential - part 8 - talking to c# library

When I found that syntax in f# to support curry, I am very excited. May be we can do the same thing with .net library written in c#. For example, maybe I can use Console.WriteLine as a function to write the following code.

let greet = Console.WriteLine "hello, {0}"
greet "fred"
Console.WriteLine "hello, {0}" "fred"

The compiler give me an error "The value is not a function and can not be applied". So F# function is different from c# method. To call this method, you have to use a tuple syntax. But you can wrap a method easily into a f# function, then you can do curry

Console.WriteLine ("hello, {0}", "fred")

//wrap to a f# function
let writeline message (name: string) = 
    Console.WriteLine(message, name)

let greet = writeline "hello, {0}"
greet "fred"

No comments:

Post a Comment