pellekrogholt

Untitled

Oct 20th, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. /*
  2.  
  3. Exercise 1: Functional programming using delegates and generics
  4. ===============================================================
  5.  
  6.  
  7. Exercise 1.3 (mandatory) Construct a generic method Compose that takes two functions and composes them.
  8.  
  9. For example, if f has type Func<int, int> then Compose(f,f) should also be of type Func<int,
  10. int> and should actually be the same as Twice(f) known from the slides. But we should be able to compose
  11. any pair of functions if only the return type of the first matches the input type of the second.
  12.  
  13. Suppose now, U is a subclass of T and f has type Func<T,T> and g has type Func<U,U>. Which of the
  14. expressions Compose(g,f) and Compose(f,g) is welltyped? Justify your answer.
  15.  
  16. Hint. Recall that the interface Func is declared Func<in T, out U>.
  17.  
  18.  
  19.  
  20.  
  21. */
  22. using System;
  23. using System.Collections.Generic;
  24.  
  25. namespace exercise_5
  26. {
  27.     public class Exercise_5_1_3
  28.     {              
  29.        
  30.         // from slides
  31.         public static Func<int,int> Twice(Func<int,int> f) {
  32.             return x => f(f(x));
  33.         }
  34.        
  35.         //public static Func<T, U> Compose<T, U>(Func<T, U> f, Func<T, U> g) {
  36.         // raised:
  37.         // /Users/pelle/itu/programming_language_concepts/programming_language_exercises/exercise_5/Exercise_5_1_3.cs(37,37): Error CS1594: Delegate `System.Func<T,U>' has some invalid arguments (CS1594) (exercise_5)
  38.         public static Func<T, T> Compose<T>(Func<T, T> f, Func<T, T> g) {
  39.             // f is calculated first the used and calculated by g
  40.             return x => g(f(x));
  41.         }
  42.  
  43.        
  44.         public static void Main() {            
  45.            
  46.             Console.WriteLine("Exercise_5_1_3");
  47.            
  48.            
  49.             Console.WriteLine("show and try out the Twice() from slides:");
  50.            
  51.             var fun1 = Twice(x => 3*x);
  52.            
  53.             Console.WriteLine(fun1(1)); // expected 9  based upon 3*1 = 3 -> 3*3 = 9
  54.            
  55.             var t = fun1(2);
  56.             Console.WriteLine(t); // expected 18  based upon 3*2 = 6 -> 3*6 = 18
  57.  
  58.            
  59.             Console.WriteLine("show and try out the Compose() that takes two lambda functions:");
  60.            
  61.             Func<int, int> a = x => 3*x;
  62.             Func<int, int> b = x => 3+x;
  63.            
  64.             Func<int, int> compose1 = Compose(a, b);
  65.             Console.WriteLine(compose1(3)); // expected 9  based upon 3*3 = 9 -> 3+9 = 12
  66.  
  67.  
  68.             // try out with mixed types int bool
  69.             // but raises:
  70.             // /Users/pelle/itu/programming_language_concepts/programming_language_exercises/exercise_5/Exercise_5_1_3.cs(52,52): Error CS0411: The type arguments for method `exercise_5.Exercise_5_1_3.Compose<T>(System.Func<T,T>, System.Func<T,T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly (CS0411) (exercise_5)
  71.  
  72.            
  73. //          try out 2:
  74. //
  75. //          Func<int, bool> c = x => x % 2 == 0;
  76. //          Func<bool, bool> d = x => x & true;
  77. //         
  78. //         
  79. //          Func<int, bool> compose2 = Compose(c, d);
  80. //
  81.            
  82.            
  83. //          try out 2:
  84. //
  85. //          Func<int, int> c = x => 3*x;
  86. //          Func<int, bool> d = x => x % 2 == 0;
  87. //         
  88. //          Func<int, bool> compose2 = Compose(c, d);
  89.            
  90.            
  91.            
  92.         }
  93.        
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment