Functional Programming with F#

Categories:

Recommended

This textbook tries to teach you functional programming, using F# as the language of instruction. F# is a multi-paradigm functional-first language: this means that it supports functional programming natively as well as the “usual” constructs such as objects and classes and interfaces and mutable variables and so on, which you may be familiar with from imperative programming. Since we are learning functional programming, we will only be using the functional parts of F#.

There are many different functional languages such as Swift, Ocaml, Haskell, Elm, Purescript, Racket, and Erlang. Some of these have strong typing, and others don’t. Some of them have algebraic data types, and others don’t. But all of them follow certain principles, and their strengths and weaknesses are directly related to these principles.

EXPRESSIONS

An expression is anything that, when evaluated, generates a value. In fact, we can think of a value as an expression that does not change its form when it is evaluated.

You may encounter non-executable code which defines a data type, or links a value to an identifier, or organizes code into different namespaces. However, all executable constructs are expressions. There is no functional equivalent of an “if-statement” or similar construct which evaluates something but doesn’t generate a value.

IMMUTABILITY

A value, once created, can never be altered.

FUNCTIONS

A function is anything which accepts exactly one input value and, when this is supplied, evaluates an expression to generate exactly one output value. The definition and application of a function are separate. Code within a function is only executed when the function is applied.

All functions are first-class: they can be used in any context that a value can be used. It can be created, it can be passed to another function, it can be given a name, it can be returned as the result of an expression, it can be part of a data structure, and so on.

REFERENTIAL TRANSPARENCY

An identifier, once it has been linked to a value, will always refer to that value. An identifier can therefore always be replaced by its value without changing the meaning of the program.

 

Category:

Attribution

Yusuf M Motara. Functional Programming Textbook. https://www.overleaf.com/read/hcwwdfxvftfp

VP Flipbook Maker

Convert your work to digital flipbook with VP Online Flipbook Maker! You can also create a new one with the tool. Try it now!