Guest User

Untitled

a guest
Nov 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace t1
  8. {
  9. static class Program
  10. {
  11.  
  12. static void Main(string[] args)
  13. {
  14. var v = new List<int>();
  15. for (var i = 0; i < 10; i++) v.Add(i);
  16. Console.WriteLine();
  17. var csum = Curry<int[], int, int>(Sum);
  18. var fix = Uncurry(IFix(csum));
  19. var r = fix(v.ToArray(), 0);
  20. Console.WriteLine(r);
  21. Console.ReadKey();
  22. }
  23.  
  24. static int Sum(Func<int[], int, int> fix, int[] array, int index)
  25. {
  26. if (index >= array.Length) return 0;
  27. return fix(array, index + 1) + 1;
  28. }
  29.  
  30. static Func<I, O> Fix<I, O>(Func<Func<I, O>, Func<I, O>> f)
  31. {
  32. //Y f = f ( Y f )
  33. return x => f(Fix(f))(x);
  34. }
  35.  
  36. static Func<Func<Tuple<I1, I2>, O>, Func<Tuple<I1, I2>, O>> Curry<I1, I2, O>(Func<Func<I1, I2, O>, I1, I2, O> f)
  37. {
  38. return y => i => f(Uncurry(y), i.Item1, i.Item2);
  39. }
  40.  
  41. static Func<I1, I2, O> Uncurry<I1, I2, O>(Func<Tuple<I1, I2>, O> f)
  42. {
  43. return (t1, t2) => f(Tuple.Create(t1, t2));
  44. }
  45.  
  46. static Func<I, O> IFix<I, O>(Func<Func<I, O>, Func<I, O>> f)
  47. {
  48. return x =>
  49. {
  50. //f(Fix(f))(x);
  51. I got = default(I);
  52. var called = true;
  53. Func<I, O> next = i =>
  54. {
  55. got = i;
  56. called = true;
  57. return default(O);
  58. };
  59. var ret = f(next)(x);
  60. while (called)
  61. {
  62. called = false;
  63. ret = f(next)(got);
  64. }
  65. return default(O);
  66. };
  67. }
  68.  
  69. }
  70. }
Add Comment
Please, Sign In to add comment