Skip to content

Instantly share code, notes, and snippets.

@cburgdorf
Created March 30, 2011 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cburgdorf/894975 to your computer and use it in GitHub Desktop.
Save cburgdorf/894975 to your computer and use it in GitHub Desktop.
comparing functional syntax between C# and F#
//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
@PHeonix25
Copy link

F#'s definition for square is wrong in the above example.
There is one too many * n's

Line 20 should read:
let square n = n * n

Thanks for the code though, perfect example of functional syntax differences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment