pellekrogholt

Untitled

Oct 20th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. /*
  2.  
  3. Exercise 1: Functional programming using delegates and generics
  4. ===============================================================
  5.  
  6. Exercise 1.1 (mandatory) Construct a generic method Forall that takes an array of type T and a predicate on
  7. T and returns a boolean. Method Forall should return true if the predicate holds for all elements in the array.
  8. Construct a Main method to test Forall. This should call Forall with a list of integers and the even predicate
  9. testing if an integer is even.
  10.  
  11. Test also Forall on an array of arrays of integers with a predicate that tests if an
  12. array has more than 2 elements.
  13.  
  14. Declare a method Exists with the same signature as Forall, but which returns true if there exists some
  15. element in the array for which the predicate is true. Test your method.
  16.  
  17. */
  18. using System;
  19.  
  20. namespace exercise_5
  21. {
  22.     public class Exercise_5_1_1
  23.     {      
  24.        
  25.         // note: just dont do Forall(.. generics requires Forall<T>(..
  26.         public static bool Forall<T>(T[] xs, Func<T, bool> p) {
  27.  
  28.             foreach(T x in xs) {               
  29.                 if (!(p(x))) return false;
  30.             }          
  31.  
  32.             return true;
  33.         }
  34.        
  35.        
  36.         public static bool Exists<T>(T[] xs, Func<T, bool> p) {
  37.  
  38.             foreach(T x in xs) {               
  39.                 if ((p(x))) return true;
  40.             }          
  41.  
  42.             return false;
  43.         }
  44.        
  45.        
  46.         public static void Main() {            
  47.                        
  48.             Console.WriteLine("create an array from 0 to 9");  
  49.            
  50.             int[] a = {0,1,2,3,4,5,6,7,8,9};
  51.            
  52.             Console.WriteLine("check if the array only contains even numbers");
  53.            
  54.             Console.WriteLine(Forall(a, x => x % 2 == 0)); // expected false
  55.            
  56.             Console.WriteLine("check if the array only contains odd numbers"); 
  57.            
  58.             Console.WriteLine(Forall(a, x => x % 2 != 0)); // expected false           
  59.            
  60.            
  61.             Console.WriteLine("create an array from 0 to 8 - only with even numbers"); 
  62.            
  63.             int[] evenNumbers = {0,2,4,6,8};
  64.  
  65.             Console.WriteLine("check if the array only contains even numbers");
  66.            
  67.             Console.WriteLine(Forall(evenNumbers, x => x % 2 == 0)); // expected true          
  68.            
  69.             Console.WriteLine("check if the array only contains odd numbers"); 
  70.            
  71.             Console.WriteLine(Forall(evenNumbers, x => x % 2 != 0)); // expected false         
  72.            
  73.  
  74.             Console.WriteLine("create an array from 1 to 9 - only with odd numbers");  
  75.            
  76.             int[] oddNumbers = {1,3,5,7,9};
  77.            
  78.             Console.WriteLine("check if the array only contains even numbers");
  79.            
  80.             Console.WriteLine(Forall(oddNumbers, x => x % 2 == 0)); // expected false          
  81.            
  82.             Console.WriteLine("check if the array only contains odd numbers"); 
  83.            
  84.             Console.WriteLine(Forall(oddNumbers, x => x % 2 != 0)); // expected true
  85.            
  86.             Console.WriteLine("test the arrays in arrays - create one array also containg an array of =< 2 elements and another array nly containing arrays > 2 elements");
  87.            
  88.             int[] smallA = {1};
  89.             int[] smallA2 = {1,2};
  90.             int[][] multiple = {a, evenNumbers, oddNumbers, smallA, smallA2};
  91.             int[][] multiple2 = {a, evenNumbers, oddNumbers};
  92.            
  93.             Console.WriteLine(Forall(multiple, x => x.Length > 2)); // expected false
  94.            
  95.             Console.WriteLine(Forall(multiple2, x => x.Length > 2)); // expected true
  96.            
  97.             Console.WriteLine("test the Exists() ");   
  98.            
  99.             Console.WriteLine(Exists(oddNumbers, x => x == 9)); // expected true
  100.            
  101.             Console.WriteLine(Exists(oddNumbers, x => x == 0)); // expected false
  102.            
  103.            
  104.             Console.WriteLine("test the Exists() with an array of strings");   
  105.            
  106.             string[] s = {"c#", "java", "python", "lisp"};
  107.            
  108.             Console.WriteLine(Exists(s, x => x.Equals("python"))); // expected true
  109.            
  110.             Console.WriteLine(Exists(oddNumbers, x => x.Equals("perl"))); // expected false
  111.            
  112.         }
  113.        
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment