# Foundations of F', 1st Edition by Robert Pickering

By Robert Pickering

Similar education & training books

Surgery at a Glance

This new version of surgical procedure at a look presents a concise and visually-orientated precis of a entire lecture path in surgical procedure. Following the easy-to-use at a look layout, each one subject is gifted with transparent illustrations and key proof encapsulating all that you have to be aware of. The e-book is coherently divided into medical shows through significant surgical stipulations.

Health Promotion: A Psychosocial Approach (Health Psychology)

"Stephens' very important and well timed booklet presents an urgently wanted and insightful synthesis of the formerly fragmented box of neighborhood healthiness psychology. a variety of case fabric from either wealthy and terrible nations is framed inside of a skilfully articulated set of debates round center problems with idea, perform, learn and ethics.

Extra info for Foundations of F', 1st Edition

Sample text

The following example shows defining a custom operator, +:*, that adds its operands and then multiplies them: #light let ( +:* ) a b = (a + b) * a * b printfn "(1 +:* 2) = %i" (1 +:* 2) The results of this example, when compiled and executed, are as follows: (1 +:* 2) = 6 Unary operators always come before the operand; binary operators are always infix. qxp 28 4/27/07 12:59 PM Page 28 CHAPTER 3 ■ FUNCTIONAL PROGRAMMING Lists F# lists are simple collection types that are built into F#. An F# list can be an empty list, represented by square brackets ([]), or it can be another list with a value concatenated to it.

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#.

To illustrate the syntax, the next example shows an implementation of a function that will produce the Lucas numbers, a sequence of numbers as follows: 1, 3, 4, 7, 11, 18, 29, 47, 76, …. The Lucas sequence has the same definition as the Fibonacci sequence; only the starting points are different. #light let rec luc x = match x with | x when x <= 0 -> failwith "value must be greater than 0" | 1 -> 1 | 2 -> 3 | x -> luc (x - 1) + luc (--x - 2) printfn printfn printfn printfn "(luc "(luc "(luc "(luc 2) = %i" (luc 2) 6) = %i" (luc 6) 11) = %i" (luc 11) 12) = %i" (luc 12) The results of this example, when compiled and executed, are as follows: (luc (luc (luc (luc 2) = 3 6) = 18 11) = 199 12) = 322 This syntax for pattern matching is the keyword match followed by the identifier that will be matched and then the keyword with.