(********* Exercice 1 *********) let i = 0 let f j = let i = i + j in i;; f 1;; (* - : int = 1 *) f 2;; (* - : int = 2 *) let i = 421;; f 1;; (* - : int = 1 *) (********* Exercice 2 *********) using System; class Program { public delegate int TwoInts (int v1, int v2); public static TwoInts plus = (i,j) => i+j; public delegate int Apply (TwoInts f, int v1, int v2); public static Apply app = (f,i,j) => f(i,j); public static void Main() { Console.WriteLine("Result : {0}", app(plus,2,3)); } } using System; using Gtk; public class Simple { static void onClick (object obj, EventArgs args) { Console.WriteLine("I have been clicked by {0}", obj); } public static void Main() { Application.Init(); //Create the Window Window myWin = new Window("Brave new world"); myWin.Resize(200,200); HBox myBox = new HBox (false, 10); myWin.Add(myBox); // Set up a button object. Button ping = new Button ("Ping !"); Button pong = new Button ("Pong ?"); pong.Sensitive = false; myBox.Add(ping); myBox.Add(pong); Action e = (obj,args) => { Button btn = (Button) obj; Console.WriteLine("I was called from a {0} called {1}", obj, btn.Label); Button otherbtn; if (btn.Label == "Ping !") { otherbtn = pong; btn.Label = "Ping ?"; btn.Sensitive = false; otherbtn.Label = "Pong !"; otherbtn.Sensitive = true; } else { otherbtn = ping; btn.Label = "Pong ?"; btn.Sensitive = false; otherbtn.Label = "Ping !"; otherbtn.Sensitive = true; } }; ping.Clicked += new EventHandler (e); pong.Clicked += new EventHandler (e); // Test the closure behavior by changing the reference // pong = new Button("Pang"); //Show Everything myWin.ShowAll(); Application.Run(); } } (********* Exercice 3 *********) import java.util.Arrays; import java.util.List; import java.util.function.*; class Functional { public static void main(String[] args) { List lis = Arrays.asList(7,4,666,9,41,5); Consumer c = (Integer t) -> { System.out.print(t + " "); }; // For loop iteration for (Integer elem : lis) System.out.print(elem + " "); System.out.println(); // Lambda iteration lis.forEach(elem -> System.out.print(elem + " ")); System.out.println(); // Sorting lis.sort( (Integer u, Integer v) -> { return u.compareTo(v); }); lis.forEach(elem -> System.out.print(elem + " ")); System.out.println(); // External variable capture Double pi = 3.14; Function f = (x) -> { return x + pi; }; System.out.println(f.apply(new Double(2))); // pi += 1; // Error: local variables referenced from a lambda expression // must be final or effectively final }; }