# free 2d barcode generator asp.net I FUNCTIONAL PROGRAMMING in Font Generation Data Matrix 2d barcode in Font I FUNCTIONAL PROGRAMMING

CHAPTER 3 I FUNCTIONAL PROGRAMMING
DataMatrix Creator In None
Using Barcode creation for Font Control to generate, create Data Matrix 2d barcode image in Font applications.
www.OnBarcode.com
Encoding Data Matrix In None
Using Barcode creator for Font Control to generate, create Data Matrix ECC200 image in Font applications.
www.OnBarcode.com
Recursion
Code 128 Code Set C Maker In None
Using Barcode creation for Font Control to generate, create Code128 image in Font applications.
www.OnBarcode.com
Barcode Printer In None
Using Barcode drawer for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Recursion means defining a function in terms of itself; in other words, the function calls itself within its definition. Recursion is often used in functional programming where you would use a loop in imperative programming. Many believe that algorithms are much easier to understand when expressed in terms of recursion rather than loops. To use recursion in F#, use the rec keyword after the let keyword to make the identifier available within the function definition. The following example shows recursion in action. Notice how on the fifth line the function makes two calls to itself as part of its own definition. #light let rec fib x = match x with | 1 -> 1 | 2 -> 1 | x -> fib (x - 1) + fib (x - 2) printfn "(fib 2) = %i" (fib 2) printfn "(fib 6) = %i" (fib 6) printfn "(fib 11) = %i" (fib 11) The results of this example, when compiled and executed, are as follows: (fib 2) = 1 (fib 6) = 8 (fib 11) = 89 This function calculates the nth term in the Fibonacci sequence. The Fibonacci sequence is generated by adding the previous two numbers in the sequence, and it progresses as follows: 1, 1, 2, 3, 5, 8, 13, . Recursion is most appropriate for calculating the Fibonacci sequence, because the definition of any number in the sequence, other than the first two, depends on being able to calculate the previous two numbers, so the Fibonacci sequence is defined in terms of itself. Although recursion is a powerful tool, you should always be careful when using it. It is easy to inadvertently write a recursive function that never terminates. Although intentionally writing a program that does not terminate is sometimes useful, it is rarely the goal when trying to perform calculations. To ensure that recursive functions terminate, it is often useful to think of recursion in terms of a base case and the recursive case. The recursive case is the value for which the function is defined in terms of itself; for the function fib, this is any value other than 1 and 2. The base case is the nonrecursive case; that is, there must be some value where the function is not defined in terms of itself. In the fib function, 1 and 2 are the base cases. Having a base case is not enough in itself to ensure termination. The recursive case must tend toward the base case. In the fib example, if x is greater than or equal to 3, then the recursive case will tend toward the base case because x will always become smaller and at some point reach 2. However, if x is less than 1, then x will grow continually more negative, and the function will recurse until the limits of the machine are reached, resulting in a stack overflow error (System.StackOverflowException).
Encoding EAN 13 In None
Using Barcode generation for Font Control to generate, create EAN13 image in Font applications.
www.OnBarcode.com
ANSI/AIM Code 39 Drawer In None
Using Barcode creation for Font Control to generate, create Code 3 of 9 image in Font applications.
www.OnBarcode.com
CHAPTER 3 I FUNCTIONAL PROGRAMMING
Barcode Creation In None
Using Barcode printer for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Making Planet In None
Using Barcode printer for Font Control to generate, create USPS Confirm Service Barcode image in Font applications.
www.OnBarcode.com
The previous code also uses F# pattern matching, which I discuss in the Pattern Matching section later in this chapter.
Paint ECC200 In None
Using Barcode encoder for Software Control to generate, create Data Matrix image in Software applications.
www.OnBarcode.com
Scanning Data Matrix ECC200 In .NET Framework
Using Barcode recognizer for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Anonymous Functions
Draw USS Code 39 In Java
Using Barcode drawer for BIRT Control to generate, create Code-39 image in Eclipse BIRT applications.
www.OnBarcode.com
Encoding UPC - 13 In Visual Basic .NET
Using Barcode creator for .NET Control to generate, create UPC - 13 image in .NET framework applications.
www.OnBarcode.com
F# provides an alternative way to define functions using the keyword fun; you can see this in the following example. Ordinarily, you would use this notation when it is not necessary to give a name to a function, so these are referred to as anonymous functions and sometimes called lambda functions or even just lambdas. The idea that a function does not need a name may seem a little strange, but if a function is to be passed as an argument to another function, then often you don t need to give it a name of its own. I demonstrate this idea in the section Lists later in this chapter when you look at operations on lists. The arguments defined for this style of function can follow the same rules as when defining a function with a name, meaning that you can define the arguments so they can be partially applied or defined in the form of a tuple (see the section Defining Types later in this chapter for an explanation of tuples). The following example shows two functions that are created and then immediately applied to arguments so that the identifier x holds the result of the function rather than the function itself: #light let x = (fun x y -> x + y) 1 2 You can create an anonymous function with the keyword function. Creating functions this way differs from using the keyword fun since you can use pattern matching when using the keyword function (see the section Pattern Matching later in this chapter). Consequently, it can be passed only one argument, but you can do a couple of things if the function needs to have multiple parameters. The first line of the following example shows a function using the function keyword written so the function can be partially applied, meaning the arguments can be passed one at a time if needed. The second line shows a function defined using the function keyword that takes a tuple argument. let x1 = (function x -> function y -> x + y) 1 2 let x2 = (function (x, y) -> x + y) (1, 2) The keyword fun is generally preferred when defining anonymous functions because it is more compact; you can see this is the case when browsing the source for the libraries and examples distributed with F#.
PDF-417 2d Barcode Reader In .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
UCC.EAN - 128 Scanner In C#.NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Decoding Data Matrix In VB.NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Draw Barcode In None
Using Barcode generator for Microsoft Excel Control to generate, create Barcode image in Excel applications.
www.OnBarcode.com
Encoding PDF417 In Java
Using Barcode maker for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
Encode UPCA In VS .NET
Using Barcode creator for .NET framework Control to generate, create UPC Symbol image in .NET framework applications.
www.OnBarcode.com
QR Encoder In None
Using Barcode generator for Excel Control to generate, create QR Code image in Microsoft Excel applications.
www.OnBarcode.com
Printing PDF 417 In .NET
Using Barcode creator for .NET Control to generate, create PDF-417 2d barcode image in .NET applications.
www.OnBarcode.com