
MANNING
With examples in F# and C#
Tomas Petricek
WITH
Jon Skeet
FOREWORD BY MADS TORGERSEN

Quick Reference – Functional Programming Concepts
IMMUTABILITY TYPES
Everything in functional programming is immutable,
which means that it can’t change once it’s created.
This rule applies to values of variables as well as to
the state stored in data types (objects) and has sev-
eral important implications.
Every expression has a type, which specifies
what kind of values we get by evaluating it. In func-
tional languages every piece of code is an expres-
sion, so types ensure that the whole program is
composed correctly.
Instead of variables, we use values (section 2.2.1),
which are immutable (3.1). Data structures are
immutable too (2.2.2, 3.2), which makes under-
standing programs easier (1.3.3). Immutability
is important for refactoring (11.1) and unit
testing (11.2), and it allows us to optimize pro-
grams using lazy evaluation (11.3). Immutability
also plays an important role when we parallelize
code (1.4.3, 14.1.3, 14.3).
The type of an expression can be a primitive (4.2.2)
or a composed. Composed types can combine mul-
tiple values (3.2, 5.2) or represent one of several
alternatives (5.3). Composed values can be
decomposed using pattern matching (3.2.4, 4.2.2,
and 5.3.2). Common types for representing data
are functional lists (3.3, 6.7), arrays (10.2.3), and
object types (chapter 9). In F#, we can annotate
type with unit of measure (2.4.4, 13.4.2) to clarify
meaning of numeric values.
DECLARATIVE STYLE EXPRESSIVITY
Code is written as an expression that describes the
desired results. Expressions can be easily and
safely composed, so we can build complex pro-
grams while hiding the details about the execution.
Recursion is a primary control structure, which
makes it easy to encode many informal specifica-
tions. It can be hidden inside primitive library func-
tions that are smoothly integrated and appear as
language extensions.
Code written as an expression (2.2.4) can be eas-
ily composed (1.4.1), analyzed (2.2.5, 6.4.3) and
encapsulated into functions (3.1, 5.5). Functions
are values (2.3.1, 3.4) and can be passed as an
argument to higher-order functions (2.3.2). Com-
plex algorithms can be built using function com-
position (6.5), pipelining (6.7), or using custom
combinators (15.1.4), for example, to describe
animations (15.5) or financial contracts (15.6).
Declarative code can be also easily parallelized
(14.1.2).
Recursive types (3.3) are an elegant way for repre-
senting data like lists (6.7.1), trees (8.4.1), or doc-
uments (7.3). We can process them using recur
sive
functions (3.3.4) that can be further optimized
(10.1). Recursion is also useful when encoding
state machines (16.3). Common functions (6.8)
are shared by many types (options [6.4], lists [6.7],
but also events [16.1]). One recurring pattern is
called monad (12.4), which can be used, for exam-
ple, for list processing (12.1–3), logging (12.7),
and asynchronous programming (13.1).
Licensed to SQUALL SQUALL <SQUALL0108@163.COM>

Real-World
Functional Programming
Licensed to SQUALL SQUALL <SQUALL0108@163.COM>

Licensed to SQUALL SQUALL <SQUALL0108@163.COM>

Real-World
Functional Programming
WITH EXAMPLES IN F# AND C#
TOMAS PETRICEK
with JON SKEET
MANNING
Greenwich
(74° w. long.)
Licensed to SQUALL SQUALL <SQUALL0108@163.COM>
评论16
最新资源