I am enjoying functional programs. This is a place to throw some interesting tidbits regarding FP. Any comments welcome on code improvements, other ideas, etc. My language of choice is C#, but feel free to post in whatever language you enjoy. This is written in a continuation style code. Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ContinuationConsoleApplication { class Program { static void Main() { Test(); Console.ReadLine(); } static T Max< T >(T v1, T v2) where T : IComparable< T > { return (v1.CompareTo(v2) > 0 ? v1 : v2); } static void Compare< T >(T n, T m, Func< T, T, T > compare, Action< T > k) { k(compare(n, m)); } static void TestContinuation< T >(T a, T b, Func< T, T, T > comparer, Action< T > k) where T : IComparable< T > { Compare(a, b, comparer, k); } static void Test() { TestContinuation(3, 4, Max, s => Console.WriteLine(s)); } } }
You can remove all the where clauses if you change Max to the following, but I am not sure I like it: Code: public static T Max<T>(T x, T y) { return (Comparer<T>.Default.Compare(x, y) > 0) ? x : y; }
i like functional programming because its different but I never seem to use a true FP language. I tend to use pythons lambda and perl closures in the real world. heres some ocaml Code: type instrument = EQUITY | MEZZANINE | JUNK | SENIOR ;; let tier stuff = match stuff with EQUITY -> 0 | JUNK -> 1 | MEZZANINE -> 2 | SENIOR -> 3 ;; let compare_instrument i i' = compare (tier i) (tier i');; compare_instrument JUNK MEZZANINE;;