Declaring and calling inner functions sample - vilinski/nemerle GitHub Wiki

Declaring and calling inner functions

  • Category: Functions
  • Description: Declaring and calling functions within the body of another function
  • Code:
using System.Console;


 def FunctionSample2()
 {
    def even(n) { n % 2 == 0 }
    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) }
    def ticktock = choose(even, tick, tock, _);  // ticktock is a function built out of other functions using 'choose'
    foreach (i in [0..10]) 
       ticktock(i);    
}

FunctionSample2();
  • 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.
⚠️ **GitHub.com Fallback** ⚠️