Created
March 30, 2011 18:41
-
-
Save cburgdorf/894975 to your computer and use it in GitHub Desktop.
comparing functional syntax between C# and F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Functional Style in C# | |
public void PrintSquares (string message, int n1, int n2) | |
{ | |
Func<int,int> square = x => x * x; | |
Action<int> printSquare = (x) => Console.WriteLine(string.Format("{0} {1}: {2}", message, x, square(x))); | |
printSquare(n1); | |
printSquare(n2); | |
} | |
//Use it like: PrintSquares("Square of", 14, 27); | |
//Prints: | |
//Square of 14: 196 | |
//Square of 27: 729 | |
//Functional Style in F# | |
let printSquares message n1 n2 = | |
let square n = n * n * n | |
let printSquare n = | |
printfn "%s %d: %d" message n (square n) | |
printSquare(n1); | |
printSquare(n2); | |
//Use it like: printSquares "Square of" 14 27 | |
//Prints: | |
//Square of 14: 196 | |
//Square of 27: 729 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
F#'s definition for square is wrong in the above example.
There is one too many
* n
'sLine 20 should read:
let square n = n * n
Thanks for the code though, perfect example of functional syntax differences.