2. F#
F# is a multiparadigm
programming language
built on .NET
3. F#
F# is a scalable, simple, succinct,
type-safe, type-inferred, practical,
efficiently executing functional-
first/imperative/object –oriented,
explorative
Open Source programming language
built on .NET
4. F# and Open Source
F# 4.0 compiler + library
Open Source
Apache 2.0 license
10. Types of Defined Entities
F# manages to find types automatically:
This is called type inference
For instance
let pi = 3.14159
Here, pi will automatically obtain the type
float since 3.14159 can only have type float
11. Explicit Type Declarations
let sum x y = x + y
F# will assume that “+” is plus on the
type int.
let sum x y = (x + y) : float
It forces the function to have type
float
13. The F# failwith Function
F# has an failwith function, which when executed prints a string and stops the
execution.
let rec sqr n:float =
if n<0. then
failwith("Cannot compute the square root of a negative number in R")
else
Math.Sqrt n
14. Interop from F#
Use #r to reference a DLL .
Open the namespace
#r "System.Core.dll"
open System.Collections.Generic
let a = HashSet<int>()
21. Focus on how to
do something
Change of state
Procedural
programming is a
common sub-type
You can have
function calls or
sub-routines
Frequently
combined with
object-oriented
programming
Imperative language
28. C#
C# doesn’t allow the definition of
a function outside a class. So the
best practice is to create a static
function (method) into a class.
29. Sum of n integers with C#
int sum = 0;
int[] numbers = { 2,5,6,3,7,38,555,66 };
foreach (var item in numbers)
{
sum += item;
}
30. Imperative programming
open System.IO
open System.Net
let url = @"http://mahamudra.it/"
// Download the webpage
let req = WebRequest.Create(url)
let resp = req.GetResponse()
let stream = resp.GetResponseStream()
let reader = new StreamReader(stream)
let html = reader.ReadToEnd()
31. Syntactic sugar with C#
var url = @"http://mahamudra.it/";
var html = string.Empty;
// Download the webpage
var req = WebRequest.Create(url);
var resp = req.GetResponse();
using (var stream = resp.GetResponseStream())
{
using (var reader = new StreamReader (stream))
{
html = reader.ReadToEnd();
}
}
33. Procedural Programming
open System.IO
open System.Net
let getHtml url
// Download the webpage
let req = WebRequest.Create(url)
let resp = req.GetResponse()
let stream = resp.GetResponseStream()
let reader = new StreamReader(stream)
let html = reader.ReadToEnd()
html
35. Functional Ideas
First-class Functions /
Higher-order Functions
• Functions can be passes as
arguments to other
functions
Purity
• Function calls don’t change
state
Immutable Data
• Data structures cannot be
modified
Reference
transparency
• Function results depend
only on its arguments
41. •A function f from a set X (the domain) to a set Y
(the range) maps each element x in X to a unique
element y in Y.
•For example, f(x) = x2 maps the set of real
numbers into the set of positive real numbers.
•i.e., the domain X is the set of all real
numbers, the range is the set of positive reals.
Background: Functions
44. •If f is a function from X to Y and g is a function from Y
to Z, then (g ◦ f ) (x) is a function from X to Z defined
as
(g ◦ f ) (x) = g(f (x)), for all x in X
Simply put, it means to replace the x in g(x) with f (x).
•Example:
•f (x) = x2 + x; g(x) = 2x + 1
•g ◦ f = 2*f(x) + 1 = 2(x2 + x ) + 1
Background: Functional Composition
45. •Example using mathematical functions:
•f (x) = x2 + x
•g(x) = 2x + 1
•g ◦ f = 2(x2 + x ) + 1
•Example using programming language function
•int f (x) {x2 + x};
•int g(x) {2x + 1};
•Now, g(f(x))is {2 * f(x) + 1};
Background: Functional Composition
46. •Pure functional programming is state-free: no
assignment statements.
•In other words, no variables, no way to permanently
change memory.
•Programs in pure functional languages consist of
composite functions; output of each function becomes
input to another.
•Today, most functional languages have some imperative
statements.
Almost functional language
55. Void vs Unit
Unit Types
// define a function returns unit
let f x = () // f : 'a -> unit
// use ignore to throw away the keyboard
input and f2 returns unit
let f2 () = System.Console.ReadKey() |>
ignore
56. Side effects
f(5) + f(5) <> 2*f(5)
Side effects requires a more
complex model, and thus makes
it harder to understand the
software
66. In imperative programming, variables are used to
denote memory locations:
x = x + 1
means “Update the program state by adding 1 to
the value stored in the memory cell named x and
storing the sum back in the memory cell.”
x is used two ways: as an r-value and an l-
value
Semantics of Variables
67. • Functional languages adopt the mathematical notion of
a variable as something that represents an expression.
•There is no association with a memory location or
modification of a variable’s value.
• So in functional languages a variable represents an
immutable value.
•Since variables don’t get updated, there’s no concept
of assignment and consequently no notion of program
state.
Semantics of Variables
69. Fun isn’t fun
This means that fun returns different values for
different calls, even when called with the same
argument.
Much harder to reason mathematically about such
functions: for instance, these values are different:
var result = Utility.fun(10) + Utility.fun(10);
var result = 2* Utility.fun(10) ;
72. Lazy loading (C#)
int[] list = {1,34,5,666,76};
var even_terms = from p in list where (p % 2==0) select p;
var it_does_not_matter = 1;
var count_even_terms = even_terms.Count();
it_does_not_matter.Dump();
count_even_terms.Dump();