Declaring and calling anonymous function lambda expressions - vilinski/nemerle GitHub Wiki

Declaring and calling anonymous function lambda expressions

  • Category: Functions

  • Description: Introduction to using lambda expressions

  • Code:


using System.Console;

def FunctionSample3()
{
    def tick(x) { WriteLine($"tick $x") }
    def tock(x) { WriteLine($"tock $x") }
    def choose(f, g, h, x) { if (f(x)) g(x) else h(x) }
    foreach (i in [0..10])
    {
        // This is like the previous sample, but uses an anonymous lambda expression for 
        // the function that decides whether to tick or tock.
        choose (n => n % 2 == 0, tick, tock, i)
    }
}
        

FunctionSample3(); 

Execution Result:

tick 0 
tock 1 
tick 2 
tock 3 
tick 4 
tock 5 
tick 6 
tock 7
tick 8 
tock 9 
tick 10 

  • Copyright

Samples used from “F# 3.0 Sample Pack” (http://fsharp3sample.codeplex.com/) at Codeplex OpenSource Community for non-commercial usage. All copyrights and authorship on materials this publication based on, is belongs to Microsoft corp. Copyright © 2006-2011 Microsoft Corporation, . All rights reserved. Copyright and autorship for materials in Nemerle language belongs to Nemerle Project Team. Copyright © 2008-2011 Nemerle Project Team. All rights reserved.