Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Exercise 1: Functional programming using delegates and generics
- ===============================================================
- Exercise 1.1 (mandatory) Construct a generic method Forall that takes an array of type T and a predicate on
- T and returns a boolean. Method Forall should return true if the predicate holds for all elements in the array.
- Construct a Main method to test Forall. This should call Forall with a list of integers and the even predicate
- testing if an integer is even.
- Test also Forall on an array of arrays of integers with a predicate that tests if an
- array has more than 2 elements.
- Declare a method Exists with the same signature as Forall, but which returns true if there exists some
- element in the array for which the predicate is true. Test your method.
- */
- using System;
- namespace exercise_5
- {
- public class Exercise_5_1_1
- {
- // note: just dont do Forall(.. generics requires Forall<T>(..
- public static bool Forall<T>(T[] xs, Func<T, bool> p) {
- foreach(T x in xs) {
- if (!(p(x))) return false;
- }
- return true;
- }
- public static bool Exists<T>(T[] xs, Func<T, bool> p) {
- foreach(T x in xs) {
- if ((p(x))) return true;
- }
- return false;
- }
- public static void Main() {
- Console.WriteLine("create an array from 0 to 9");
- int[] a = {0,1,2,3,4,5,6,7,8,9};
- Console.WriteLine("check if the array only contains even numbers");
- Console.WriteLine(Forall(a, x => x % 2 == 0)); // expected false
- Console.WriteLine("check if the array only contains odd numbers");
- Console.WriteLine(Forall(a, x => x % 2 != 0)); // expected false
- Console.WriteLine("create an array from 0 to 8 - only with even numbers");
- int[] evenNumbers = {0,2,4,6,8};
- Console.WriteLine("check if the array only contains even numbers");
- Console.WriteLine(Forall(evenNumbers, x => x % 2 == 0)); // expected true
- Console.WriteLine("check if the array only contains odd numbers");
- Console.WriteLine(Forall(evenNumbers, x => x % 2 != 0)); // expected false
- Console.WriteLine("create an array from 1 to 9 - only with odd numbers");
- int[] oddNumbers = {1,3,5,7,9};
- Console.WriteLine("check if the array only contains even numbers");
- Console.WriteLine(Forall(oddNumbers, x => x % 2 == 0)); // expected false
- Console.WriteLine("check if the array only contains odd numbers");
- Console.WriteLine(Forall(oddNumbers, x => x % 2 != 0)); // expected true
- 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");
- int[] smallA = {1};
- int[] smallA2 = {1,2};
- int[][] multiple = {a, evenNumbers, oddNumbers, smallA, smallA2};
- int[][] multiple2 = {a, evenNumbers, oddNumbers};
- Console.WriteLine(Forall(multiple, x => x.Length > 2)); // expected false
- Console.WriteLine(Forall(multiple2, x => x.Length > 2)); // expected true
- Console.WriteLine("test the Exists() ");
- Console.WriteLine(Exists(oddNumbers, x => x == 9)); // expected true
- Console.WriteLine(Exists(oddNumbers, x => x == 0)); // expected false
- Console.WriteLine("test the Exists() with an array of strings");
- string[] s = {"c#", "java", "python", "lisp"};
- Console.WriteLine(Exists(s, x => x.Equals("python"))); // expected true
- Console.WriteLine(Exists(oddNumbers, x => x.Equals("perl"))); // expected false
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment