Functional Programming 4 Theoretical foundation Churchs calculus expressions

  • Slides: 12
Download presentation
Functional Programming 4 Theoretical foundation – Church’s -calculus • expressions and evaluation rules 4

Functional Programming 4 Theoretical foundation – Church’s -calculus • expressions and evaluation rules 4 Characteristics – Single assignment variables (pure FP) – Recursion – Rule-based and pattern matching (ML, Haskell, F#) – High-order functions – Lazy evaluation (Haskell) – Meta-programming (Scheme) by Neng-Fa Zhou

F# 4 A hybrid language – ML-like functional programming – Iimperative programming – OOP

F# 4 A hybrid language – ML-like functional programming – Iimperative programming – OOP – Scripting 4 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# 4 Available for free – Works with Visual Studio – Standalone fsharp interpreter fsi by Neng-Fa Zhou

F# vs. Prolog 4 Common characteristics • Repetition via recursion • High-order • Garbage

F# vs. Prolog 4 Common characteristics • Repetition via recursion • High-order • Garbage collection 4 Differences • Strongly typed vs. dynamically typed • Functional vs. relational • Prolog supports unification and backtracking by Neng-Fa Zhou

Types 4 int -- 123, -10 4 float -- 122. 123, 0. 23 e-10

Types 4 int -- 123, -10 4 float -- 122. 123, 0. 23 e-10 4 bool -- true, false 4 char -- ‘a’ 4 string -- “abc” 4 list -- [1; 2; 3], 1: : [2; 3], [1]@[2; 3] 4 array -- [|1; 2; 3|] 4 tuple -- ("abc", 1, true) 4 union -- type My. Bool = | True | False by Neng-Fa Zhou

Operators 4 +, -, *, /, % 4 &&, ||, not 4 =, <>,

Operators 4 +, -, *, /, % 4 &&, ||, not 4 =, <>, <, >, <=, >= by Neng-Fa Zhou

let 4 let x = 1+2+3 4 let f x y = x+y 4

let 4 let x = 1+2+3 4 let f x y = x+y 4 let f (x, y) = x+y 4 let rec f n = if n = 0 then 1 else n*(f n-1) by Neng-Fa Zhou

Pattern Matching let rec len lst = match lst with | [] -> 0

Pattern Matching let rec len lst = match lst with | [] -> 0 | _: : lst 1 -> 1+len lst 1 by Neng-Fa Zhou

Tail Recursion let rec len 1 ac lst = match lst with | []

Tail Recursion let rec len 1 ac lst = match lst with | [] -> ac | (_: : lstr) -> len 1 (ac+1) lstr let len lst = len 1 0 lst by Neng-Fa Zhou

Unions type SExp = |O | S of Sexp let rec sum x y

Unions type SExp = |O | S of Sexp let rec sum x y = match x with | O -> y | S x 1 -> S(sum x 1 y) by Neng-Fa Zhou

Unions (Cont. ) type Tree. Int = | Void | Leaf of int |

Unions (Cont. ) type Tree. Int = | Void | Leaf of int | Node of int*Tree. Int let rec count tree = match tree with | Void -> 0 | Leaf(_) -> 1 | Node(_, left, right) -> 1+ (count left) + (count right) by Neng-Fa Zhou

High-order Functions let succ = fun x -> x+1 List. map succ [1; 2;

High-order Functions let succ = fun x -> x+1 List. map succ [1; 2; 3] List. map (fun x -> x+1) [1; 2; 3] by Neng-Fa Zhou

map and fold let rec map f lst = match lst with | []

map and fold let rec map f lst = match lst with | [] -> [] | (car : : cdr) -> (f car): : (map f cdr) let rec fold f lst acc = match lst with | [] -> acc | (car : : cdr) -> f car (fold f cdr acc) let rec foldl f lst acc = match lst with | [] -> acc | (car : : cdr) -> foldl f cdr (f car acc) by Neng-Fa Zhou