Title: Functional Programming
1Functional Programming
- Theoretical foundation
- Churchs ?-calculus
- expressions and evaluation rules
- Characteristics
- Single assignment variables (pure FP)
- Recursion
- Rule-based and pattern matching (ML, Haskell, F)
- High-order functions
- Lazy evaluation (Haskell)
- Meta-programming (Scheme)
2F
- A hybrid language
- ML-like functional programming
- Iimperative programming
- OOP
- Scripting
- Runs on the .NET platform
- It is possible to use any .NET library from F
- It is possible to use F library from other .NET
languages such as C - Available for free
- Works with Visual Studio
- Standalone fsharp interpreter fsi
3F vs. Prolog
- Common characteristics
- Repetition via recursion
- High-order
- Garbage collection
- Differences
- Strongly typed vs. dynamically typed
- Functional vs. relational
- Prolog supports unification and backtracking
4Types
- int -- 123, -10
- float -- 122.123, 0.23e-10
- bool -- true, false
- char -- a
- string -- abc
- list -- 123, 123, 1_at_23
- array -- 123
- tuple -- ("abc",1,true)
- union -- type MyBool True False
5Operators
- , -, , /,
- , ,not
- , ltgt, lt, gt, lt, gt
6let
- let x 123
- let f x y xy
- let f (x,y) xy
- let rec f n if n 0 then 1 else n(f n-1)
7Pattern Matching
let rec len lst match lst with -gt 0
_lst1 -gt 1len lst1
8Tail Recursion
let rec len1 ac lst match lst with
-gt ac (_lstr) -gt len1 (ac1) lstr let
len lst len1 0 lst
9Unions
type SExp O S of Sexp let rec sum x y
match x with O -gt y S x1 -gt S(sum
x1 y)
10Unions (Cont.)
type TreeInt Void Leaf of int
Node of intTreeIntTreeInt let rec count tree
match tree with Void -gt 0 Leaf(_)
-gt 1 Node(_,left,right) -gt 1 (count left)
(count right)
11High-order Functions
let succ fun x -gt x1 List.map succ
123 List.map (fun x -gt x1) 123
12map and fold
let rec map f lst match lst with
-gt (car cdr) -gt (f car)(map f
cdr) let rec fold f lst acc match lst
with -gt acc (car cdr) -gt f car
(fold f cdr acc) let rec foldl f lst acc
match lst with -gt acc (car
cdr) -gt foldl f cdr (f car acc)