Nov 2005 SDP-MSc Slide 1 Section 10 Functional Programming in C#

Slides:



Advertisements
Similar presentations
Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Advertisements

CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Sept 2004 SDP-MSc Slide 1 Section 6 Introduction to Functional Programming.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
ILM Proprietary and Confidential -
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Going Functional Primož Gabrijelčič. Functional programming.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Exercise 1 : ex1.java class C extends Thread { int i; C(int i) { this.i = i; } public void run() { System.out.println("Thread " + i + " says hi"); System.out.println("Thread.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion Reading L&C 3 rd : 7.1 – nd :
Application development with Java Lecture 6 Rina Zviel-Girshin.
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Methods.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Dec 2005 MSc Slide 1 Pattern that are most specifically concerned with communication between objects Cat 3: Behavioral Patterns.
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Conditional Expressions
Principles of programming languages 4: Parameter passing, Scope rules
A lightening tour in 45 minutes
Haskell.
Stacks.
Interfaces and Constructors
PROGRAMMING IN HASKELL
Stacks.
Code Refresher Test #1 Topics:
Week 4 Lecture-2 Chapter 6 (Methods).
Object Oriented Programming
Test Review CIS 199 Exam 2 by.
Exercise 1 : ex1.java Thread Creation and Execution
Main() { int fact; fact = Factorial(4); } main fact.
Exercise 1 : ex1.java Thread Creation and Execution (sleep() and join()) class C extends Thread {   int i;   C(int i) { this.i = i; }   public void run()
Methods/Functions.
PROGRAMMING IN HASKELL
Presentation transcript:

Nov 2005 SDP-MSc Slide 1 Section 10 Functional Programming in C#

Nov 2005 SDP-MSc Slide 2 References Java as a Functional Programming Language - A.Setzer Higher-Order Functions as Objects

Nov 2005 SDP-MSc Slide 3 Principles Reviewed 3 No Side effects No variable assignment A Function has no effect other than calculating its value

Nov 2005 SDP-MSc Slide 4 Key Features- Functional Programs 4 Higher Order Functions Lazy Evaluation Currying Lambda Expressions List Processing

Nov 2005 SDP-MSc Slide 5 Functional Programs 5 Program consists only of Functions Main program is a function, receives input as parameters May in turn be defined as other functions

Nov 2005 SDP-MSc Slide 6 - No side effects 6 class Fun2 { public static int res; // global variable public static void sum(int a, int b){ res= a+b;} // side effect public static void Main(string[] args) { int x=Convert.ToInt32(args[0]); int y=Convert.ToInt32(args[1]); sum(x,y); Console.WriteLine("Sum of values:{0}", res); }

Nov 2005 SDP-MSc Slide 7 Function result only depends on input parameters Always returns the same result, whenever called Order of function calls irrelevant: f(x)=2*x + 1 => f(2)=5 always Functional Programming -

Nov 2005 SDP-MSc Slide 8 No Side-effects - Summary No global variables No variable assignment Function just calculates a value based on input parameters Next: Another counter example

Nov 2005 SDP-MSc Slide 9 class Fact1 { public static int factorial(int n){ int res=1; for (int i=1;i<=n;i++) res*=i; // var assignment return res;} public static void Main(string[] args) { int x=Convert.ToInt32(args[0]); Console.WriteLine("Factorial= {0}", factorial(x)); }

Nov 2005 SDP-MSc Slide 10

Nov 2005 SDP-MSc Slide 11 Recursion Most Functional Programming Languages use recursion - avoids variable assignment - facilitates Lazy Evaluation

Nov 2005 SDP-MSc Slide 12 class Fact2 { public static int factorial(int n){ if (n==0) return 1; else return n * factorial(n-1); } public static void Main(string[] args) { int x=Convert.ToInt32(args[0]); Console.WriteLine("Factorial= {0}", factorial(x)); }

Nov 2005 SDP-MSc Slide 13 Functional Programming Two other key features - List Processing - Polymorphic Types

Nov 2005 SDP-MSc Slide 14 class Vec3{ public static int total(ArrayList vec){ int res=0; for(int i=0; i<vec.Count;i++) { res+= (int)vec[i]; } return res;} public static void Main() { ArrayList v=new ArrayList(); v.Add(1); v.Add(2); v.Add(3); Console.WriteLine("Sum of Elements: {0}", total(v));} }

Nov 2005 SDP-MSc Slide 15 Next Fuctional Version

Nov 2005 SDP-MSc Slide 16 class Vec4 {public static int convert(Object o){ return ((Integer)o).intValue();} public static int total(Vector vec,int first){ if (vec.isEmpty()) return 0; if (first==vec.size()-1) return convert(vec.elementAt(first)); else return convert(vec.elementAt(first)) + total(vec,first+1); }

Nov 2005 SDP-MSc Slide 17 : public static void main(String[] args) { Vector v=new Vector(); v.addElement(new Integer(Integer.parseInt(args[0]))); v.addElement(new Integer(Integer.parseInt(args[1]))); v.addElement(new Integer(Integer.parseInt(args[2]))); System.out.println("Sum of Elements: "+ total(v,0)); }

Nov 2005 SDP-MSc Slide 18 Now to search vector [3,4,5] for a value

Nov 2005 SDP-MSc Slide 19 Functional Programming -other key features Higher Order Functions Lazy Evaluation Currying Lambda expressions (Nameless functions) All of these depend on Higher Order Functions

Nov 2005 SDP-MSc Slide 20 Lazy Evaluation Normal Evaluation: mult (fac 3) (fac 4) Evaluate parameter first: mult 6 24 => 144 Lazy Evaluation - only evaluate (fac 3) etc when needed

Nov 2005 SDP-MSc Slide 21 Lazy Evaluation mult (fac 3) (fac 4) becomes (fac 3) * (fac 4) = 6 * 24 => 144 Lazy Evaluation - only evaluate (fac 3) etc when needed

Nov 2005 SDP-MSc Slide 22 Lazy Evaluation - C# Using ‘if’ statements in C# to implement ‘Lazy’ : if (x <10) return increment(x); else return 10;} :

Nov 2005 SDP-MSc Slide 23 Lazy Evaluation - Java Recursive Functions also facilitate Lazy Evaluation Factorial…

Nov 2005 SDP-MSc Slide 24 Lazy Evaluation - Java Ability to pass functions ‘unevaluated’ is central to ‘Lazy Evaluation’ I.e. Higher Order Functions

Nov 2005 SDP-MSc Slide 25 Higher Order Functions A Function which takes another function as parameter Function can also be returned as a result I.e Functions treated as Objects increment :: Int -> Int increment n = (n+1)

Nov 2005 SDP-MSc Slide 26 Higher Order Functions increment :: Int -> Int increment n = (n+1) map1 :: (a -> b) -> [a] -> [b] map1 f [] = [] map1 f (x:xs) = f x : map1 f xs > map1 increment [1,2,3,4] [2,3,4,5]

Nov 2005 SDP-MSc Slide 27 Higher Order Functions Two Methods in C# (1)Using interface and polymorphism: pass an Object which refers to a method (2) Using delegate type pass a reference to a function

Nov 2005 SDP-MSc Slide 28 Higher Order Functions In this section (1)Using interface and polymorphism for HOF: (2) Delegate type in general (3) Delegate type for HOF

Nov 2005 SDP-MSc Slide 29 Higher Order Functions in C# using interface using System; public interface OneArgFunction{ int apply(int x); } class Increment :OneArgFunction{ public int apply(int x){ return x+1;} } increment :: Int -> Int increment n = (n+1)

Nov 2005 SDP-MSc Slide 30 public class TestHOF1{ public static void map(OneArgFunction f, int[] a) {for (int i=0;i<a.Length;i++){ a[i]=f.apply(a[i]);} } public static void Main(string[] args){ int[] arr={3,4,5,6,7}; map(new Increment(),arr); for (int i=0; i<5;i++){ Console.WriteLine("["+arr[i]+"]");} Console.Read(); }}

Nov 2005 SDP-MSc Slide 31 Write a function which : - takes a function & list of Ints as paramemets - applies function to first & last element - return result as sum of these two wlaues >sum_fl increment [3,4,5,6] 11 i.e sum_fl :: (Int -> Int) -> [Int] -> Int sum_fl f (x:xs) = f x + f (last xs) Now the Java Version -->

Nov 2005 SDP-MSc Slide 32 using System; public interface Operation{ int apply(int x); } class Increment :Operation{ public int apply(int x){ return x+1;}} :

Nov 2005 SDP-MSc Slide 33 : public class TestHOF4{ public static int sum_fl(Operation f, int[] a) { return f.apply(a[0])+ f.apply(a[a.Length-1]); } public static void Main(string[] args){ int[] arr={3,4,5,6,7}; int res=sum_fl(new Increment(),arr); Console.WriteLine("Sum of 1st & Last= {0}",res); Console.Read(); }}

Nov 2005 SDP-MSc Slide 34 Another Example Add.op(2,3) -> 5 apply_op (Add.op, [3,4,5,6,7]) -> 25

Nov 2005 SDP-MSc Slide 35 using System; using System.Collections; public interface Operation{ int op(int x, int y); } class Add : Operation{ public int op(int x,int y){ return x+y;}}:

Nov 2005 SDP-MSc Slide 36 : public class Ex1{ public static int apply_op(Operation f, int[] a) {int res=0; for (int i=0; i<a.Length;i++) {res = f.op(res,a[i]);} return res; }

Nov 2005 SDP-MSc Slide 37 : public static void Main(string[] args){ int [] arr={3,4,5,6,7}; int res=apply_op(new Add(), arr); Console.WriteLine("Sum of elements= "+res); Console.Read();} }

Nov 2005 SDP-MSc Slide 38 Exercise 1 Large implemements Validate -- inner class testval 8 returns false testval 11 returns true - values >= 10 test_list testval [2, 12, 8, 17, 9] returns false test_list testval [12, 13, 18, 17,19] returns true

Nov 2005 SDP-MSc Slide 39 Exercise 2 Bigger implements Validate-- inner class compare 8,9 returns false compare 9,8 returns true compare 9,9 returns false test_list compare [12, 8, 17, 9] 9 returns false test_list compare [12, 8, 17, 9] 7 returns true Create 2 versions: (a) Bigger as an Inner Class (b) Anonymous (nameless Inner Class)

Nov 2005 SDP-MSc Slide 40 Filter Example Small 8 returns true Small 11 returns false myfilter small, [2, 12, 8, 17, 9], 0 returns [2, 8, 9]

using System; using System.Collections; public interface Validate{ bool testval(int n); } class Small : Validate{ public bool testval(int n){ if (n<10) return true; else return false; }

public class HOFsmall1{ public static void myFilter(Validate f, ArrayList v, int index) { if (v.Count==0) return; if (index==v.Count) return; else {int el=(int)v[index]; if (f.testval(el)==false) { v.RemoveAt(index); myFilter(f,v,index);} else myFilter(f,v,index+1);} } :

public static void Main(string[] args){ ArrayList a=new ArrayList(); a.Add(2); a.Add(19); a.Add(9); a.Add(14); a.Add(9); a.Add(8); myFilter(new Small(),a,0); for (int i=0; i<a.Count;i++){ int val=(int)a[i]; Console.WriteLine("["+val+"]");} Console.Read(); }}

Nov 2005 SDP-MSc Slide 44 Another Filter Example greater 8 9 returns false greater 9 8 returns true greater 9 9 returns false myfilter greater, [2, 12, 4, 3, 9], 3, 0 returns [12, 4, 9]

Nov 2005 SDP-MSc Slide 45 using System; using System.Collections; public interface Condition{ bool test(int x,int y); } class Greater :Condition{ public bool test(int x,int y){ if (x>y) return true; else return false; }

Nov 2005 SDP-MSc Slide 46 public class HOFgreater1{ public static void myFilter(Condition f,ArrayList v, int val,int index) { if (v.Count==0) return; if (index==v.Count) return; else { int el=(int)v[index]; if (f.test(el,val)==false) { v.RemoveAt(index); myFilter(f,v,val,index);} else myFilter(f,v,val,index+1); }

Nov 2005 SDP-MSc Slide 47 public static void Main(string[] args){ ArrayList a=new ArrayList(); a.Add(7); a.Add(19); a.Add(9); a.Add(6); a.Add(14); a.Add(24); myFilter(new Greater(),a,8,0); for (int i=0; i<a.Count;i++){ int val=(int)a[i]; Console.WriteLine("["+val+"]");} Console.Read(); }}

Nov 2005 SDP-MSc Slide 48 Modify previous Filter Example in 2 ways equal 8 9 returns false equal 9 9 returns true filter_Out equal, [3, 12, 4, 3, 9], 3, 0 returns [12, 4, 9] Exercise 3

Nov 2005 SDP-MSc Slide 49 The delegate type Pointer to function

Nov 2005 SDP-MSc Slide 50 class GFrame4 :GFrame3 { protected Button b1=new Button(); public GFrame4():base(){ b1.Text= "Change Colour"; b1.SetBounds(100,180,90,23); b1.Click += new EventHandler(this.button1_Click); Controls.Add(b1); } private void button1_Click(object sender, EventArgs e){ brush.Color=Color.Yellow; this.Refresh();} } public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame4());} }

Nov 2005 SDP-MSc Slide 51 First a Basic Example

Nov 2005 SDP-MSc Slide 52 using System; using System.Collections; public delegate void Operation(); // type definition public class Test{ public static void print(){ Console.WriteLine("Hello");} public static void Main(string[] args){ Operation o=new Operation(print); // delegate o(); Console.Read(); }}

Nov 2005 SDP-MSc Slide 53

Nov 2005 SDP-MSc Slide 54 public delegate void Operation(); public class Test{ public static void print1(){ Console.WriteLine("Hello");} public static void print2(){ Console.WriteLine("World");} public static void Main(string[] args){ Operation o=new Operation(print1); o+= new Operation(print2); o(); Console.Read(); }}

Nov 2005 SDP-MSc Slide 55 Now to Pass Pointers to Functions as Parameters

Nov 2005 SDP-MSc Slide 56 public delegate void Operation(); public class Test{ private int x=2; public void incr(){x++;} public void decr(){x--;} public void print(){ Console.WriteLine("Value={0}",x); } public void Action(Operation o){ o();} }

Nov 2005 SDP-MSc Slide 57 public class Test99{ public static void Main(string[] args){ Test t=new Test(); Operation o1=new Operation(t.incr); Operation o2=new Operation(t.print); t.Action(o1); t.Action(o2); Console.Read(); }}

Nov 2005 SDP-MSc Slide 58

Nov 2005 SDP-MSc Slide 59 public class Test99{ public static void Main(string[] args){ Test t=new Test(); Operation o1=new Operation(t.incr); o1 +=new Operation(t.print); t.Action(o1); Console.Read(); }} Alternative Test Function

Nov 2005 SDP-MSc Slide 60

Nov 2005 SDP-MSc Slide 61 public delegate void Operation(); public class Test{ private int x=2; public void incr(){x++;} public void decr(){x--;} public void print(){ Console.WriteLine("Value={0}",x); }

Nov 2005 SDP-MSc Slide 62 : public void step2() { Operation o = new Operation(incr); o(); o();} public void stepandprint(){ Operation o = new Operation(incr); o += new Operation(print); o(); }

Nov 2005 SDP-MSc Slide 63 public class Test99{ public static void Main(string[] args){ Test t=new Test(); t.print(); t.step2(); t.stepandprint(); Console.Read(); }}

Nov 2005 SDP-MSc Slide 64

Nov 2005 SDP-MSc Slide 65 Higher Order Functions Using Delegates

Nov 2005 SDP-MSc Slide 66 Higher Order Functions A Function which takes another function as parameter Function can also be returned as a result I.e Functions treated as Objects increment :: Int -> Int increment n = (n+1)

Nov 2005 SDP-MSc Slide 67 Higher Order Functions increment :: Int -> Int increment n = (n+1) map1 :: (a -> b) -> [a] -> [b] map1 f [] = [] map1 f (x:xs) = f x : map1 f xs > map1 increment [1,2,3,4] [2,3,4,5]

Nov 2005 SDP-MSc Slide 68 Higher Order Functions in C# using delegates using System; using System.Collections; public delegate int OneArgFunction(int x); public class TestHOF1{ public static int apply(int x){ return x+1;} : increment :: Int -> Int increment n = (n+1)

Nov 2005 SDP-MSc Slide 69 : public static void map(OneArgFunction f, int[] a) {for (int i=0;i<a.Length;i++){ a[i]=f(a[i]);} } public static void Main(string[] args){ int[] arr={3,4,5,6,7}; map(new OneArgFunction(apply),arr); for (int i=0; i<5;i++){ Console.WriteLine("["+arr[i]+"]");} Console.Read(); }}

Nov 2005 SDP-MSc Slide 70

Nov 2005 SDP-MSc Slide 71 Write a function which : - takes a function & list of Ints as paramemets - applies function to first & last element - return result as sum of these two wlaues >sum_fl increment [3,4,5,6] 11 i.e sum_fl :: (Int -> Int) -> [Int] -> Int sum_fl f (x:xs) = f x + f (last xs) Now the Java Version -->

Nov 2005 SDP-MSc Slide 72 public delegate int Operation(int x); public class TestHOF1{ public static int apply(int x){ return x+1;}

Nov 2005 SDP-MSc Slide 73 : public static int sum_fl(Operation f, int[] a) { return f(a[0])+ f(a[a.Length-1]); } public static void Main(string[] args){ int[] arr={3,4,5,6,7}; Operation o=new Operation(apply); int res=sum_fl(o,arr); Console.WriteLine("Sum of 1st & Last= {0}",res); Console.Read(); }}

Nov 2005 SDP-MSc Slide 74

Nov 2005 SDP-MSc Slide 75 Another Example Add.op(2,3) -> 5 apply_op (Add.op, [3,4,5,6,7]) -> 25

Nov 2005 SDP-MSc Slide 76 using System; using System.Collections; public delegate int Operation(int x, int y); public class TestHOF1{ public static int op(int x, int y){ return x+y;} :

Nov 2005 SDP-MSc Slide 77 : public static int apply_op(Operation f, int[] a) {int res=0; for (int i=0; i<a.Length;i++) {res = f(res,a[i]);} return res; } public static void Main(string[] args){ int[] arr={3,4,5,6,7}; Operation o=new Operation(op); int res=apply_op(o, arr); Console.WriteLine("Sum of elements= "+res); Console.Read(); }}

Nov 2005 SDP-MSc Slide 78

Nov 2005 SDP-MSc Slide 79 Exercise 1 Large implemements Validate – delegate type testval 8 returns false testval 11 returns true - values >= 10 test_list testval [2, 12, 8, 17, 9] returns false test_list testval [12, 13, 18, 17,19] returns true

Nov 2005 SDP-MSc Slide 80 Exercise 2 Bigger implements Validate– delegate type compare 8,9 returns false compare 9,8 returns true compare 9,9 returns false test_list compare [12, 8, 17, 9] 9 returns false test_list compare [12, 8, 17, 9] 7 returns true

Nov 2005 SDP-MSc Slide 81 Filter Example Small 8 returns true Small 11 returns false myfilter small, [2, 12, 8, 17, 9], 0 returns [2, 8, 9]

Nov 2005 SDP-MSc Slide 82 using System; using System.Collections; public delegate bool Validate(int n); public class TestHOF1{ public static bool testval(int n){ if (n<10) return true; else return false; }

Nov 2005 SDP-MSc Slide 83 public static void myFilter(Validate f, ArrayList v, int index) { if (v.Count==0) return; if (index==v.Count) return; else {int el=(int)v[index]; if (f(el)==false) { v.RemoveAt(index); myFilter(f,v,index);} else myFilter(f,v,index+1);} } :

Nov 2005 SDP-MSc Slide 84 public static void Main(string[] args){ ArrayList a=new ArrayList(); a.Add(2); a.Add(19); a.Add(9); a.Add(14); a.Add(9); a.Add(8); Validate v=new Validate(testval); myFilter(v,a,0); for (int i=0; i<a.Count;i++){ int val=(int)a[i]; Console.WriteLine("["+val+"]");} Console.Read(); }}

Nov 2005 SDP-MSc Slide 85

Nov 2005 SDP-MSc Slide 86 Another Filter Example greater 8 9 returns false greater 9 8 returns true greater 9 9 returns false myfilter greater, [2, 12, 4, 3, 9], 3, 0 returns [12, 4, 9]

Nov 2005 SDP-MSc Slide 87 public delegate bool Condition(int x, int y); public class TestHOF1{ public static bool test(int x,int y){ if (x>y) return true; else return false; }

Nov 2005 SDP-MSc Slide 88 public static void myFilter(Condition f,ArrayList v, int val,int index) { if (v.Count==0) return; if (index==v.Count) return; else { int el=(int)v[index]; if (f(el,val)==false) { v.RemoveAt(index); myFilter(f,v,val,index);} else myFilter(f,v,val,index+1); }

Nov 2005 SDP-MSc Slide 89 public static void Main(string[] args){ ArrayList a=new ArrayList(); a.Add(7); a.Add(19); a.Add(9); a.Add(6); a.Add(14); a.Add(24); Condition c=new Condition(test); myFilter(c,a,8,0); for (int i=0; i<a.Count;i++){ int val=(int)a[i]; Console.WriteLine("["+val+"]");} Console.Read(); }}

Nov 2005 SDP-MSc Slide 90

Nov 2005 SDP-MSc Slide 91 Modify previous Filter Example in 2 ways equal 8 9 returns false equal 9 9 returns true filter_Out equal, [3, 12, 4, 3, 9], 3, 0 returns [12, 4, 9] Exercise 3