F# for python programmers. Goals Goals Motivation Mapping key features Python -> F# Angles to tackle...
69
F# for python programmers
F# for python programmers. Goals Goals Motivation Mapping key features Python -> F# Angles to tackle ◦ New language ◦ New development environment ◦ New
val c : string = "big"
// An alternative construct uses the pattern match which is // more
flexible
let c2 = match n with
| x when x < 0 -> "negative"
| x when x < 10 -> "single digit"
| _ -> "big"
printf "%d\n" I
#Python
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Collections
("Fortran",3.0) ; ("COBOL",0.0) ];;
// F# assigns a type signature to each expression which you
// will see explicitly if you are using the fsi F# interactive
environment
val primeList : int list = [1; 3; 5; 7; 11; 13; 17; 19]
val primeArray : int [] = [|1; 3; 5; 7; 11; 13; 17; 19|]
val tuple : float * float = (12.3, 4.5)
val tuple2 : string * float = ("F#", 10.0)
val listOfTuples : (string * float) list =
[("F#", 10.0); ("Python", 9.0); ("Java", 5.0); ("Fortran",
3.0);
("COBOL", 0.0)]
Sequence manipulation
primeArray.[0..2] // Elements 0, 1 AND 2
primeArray.[-1] // Won’t work ;( no rhs indexing
// Also works for list but slower
primeList.[3]
// fst and snd (first and second) operators for tuples
snd tuple
List.head primeList
List.tail primeList
The last expression in a function is its return value
Unfinished Functions
# python
FILE PROCESSING EXAMPLE
fp = open (@”c:\tmp\foo.txt”,”r”)
IO libraries are part of DotNet and not strictly an F#
concept.
Easy way to process a text file
open System.IO
lines.Length
Sample Output
0.333 0.333
0.308 0.333
0.286 0.444
0.455 0.316
0.400 0.333
0.455 0.462
0.364 0.250
0.400 0.417
0.458 0.417
0.389 0.438
0.385 0.467
0.300 0.353
let rec factorial n =
else if n < 2 then 1
else n * factorial (n-1)
/// Tail recursion friendly version
if n = 1 then mult else _fact (n-1) (mult*n)
if n < 1 then
failwith "bad value"
_fact n 1
More If statements
F# has the familiar constructs for if, while and for that you find
in python with some small twists. In keeping with the functional
style of programming, the if statement returns a value
let pos f =
;;
d.[“cat”]
// sys.argv
// Random number generation
let rng = System.Random()
let i = rng.Next(100)
Map/Reduce crash course
The bread-and-butter of a functional programming language is list
processing. F# provides many facilities for taking lists and
transforming them. Returning to our primes example, we can
calculate a list of numbers that appear just before primes by
applying a simple n-1 function to our previous primes list. Here we
use an anonymous function but we could just as easily define a
previous function and use it.
let primeMinusOne = Seq.map (fun n -> n-1) primes;;
Functions are first class values
(you can pass functions to functions, make lists of functions,
dictionaries etc)
No state in strict implementations
Deemphasizes variables, loops in favor of other techniques
Lack of state leads to safer programming technique
63
(http://en.wikipedia.org/wiki/Functional_programming)
65
Strong versus Weak Typing
Enforcing type checks makes a language more reliable (but less
flexible)
int x = 5.6; // Error
def bwuuHaHaHaHa(x): if x< 0: return “cat” elif x > 0: return
3.1415926 printf “What about %d\n” % bwuuHaHaHaHa(0)
66
67
Map/Reduce
Map
Takes a collection and applies a function to every element in the
collection to make a new collection
Reduce
Serially applies a function to each element of a collection
incorporating the result of the last function into the next
Appearing in non-functional language settings
Google manages distributed data processing using 1000s of map
reduce jobs per day
http://labs.google.com/papers/mapreduce.html
68
Monads