Download presentation
Presentation is loading. Please wait.
1
Functional Programming
Theoretical foundation Church’s -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) by Neng-Fa Zhou
2
F# A hybrid language Runs on the .NET platform Available for free
ML-like functional programming Imperative 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 by Neng-Fa Zhou
3
F# vs. Prolog Common characteristics Differences
Repetition via recursion High-order Garbage collection Differences Strongly typed vs. dynamically typed Functional vs. relational Prolog supports unification and backtracking by Neng-Fa Zhou
4
Types int -- 123, -10 float -- 122.123, 0.23e-10 bool -- true, false
char -- ‘a’ string -- “abc” list -- [1;2;3], 1::[2;3], array -- [|1;2;3|] tuple -- ("abc",1,true) union -- type MyBool = | True | False by Neng-Fa Zhou
5
Operators +, -, *, /, % &&, ||,not
=, <>, <, >, <=, >= &&&, |||, ^^^, ~~~,<<<,>>> by Neng-Fa Zhou
6
let let x = 1+2+3 let f x y = x+y let f (x,y) = x+y
let rec f n = if n = 0 then 1 else n* f (n-1) by Neng-Fa Zhou
7
Pattern Matching let rec len lst = match lst with | [] -> 0
| _::lst1 -> 1+len lst1 by Neng-Fa Zhou
8
Tail Recursion let rec len1 ac lst = match lst with | [] -> ac
| (_::lstr) -> len1 (ac+1) lstr let len lst = len1 0 lst by Neng-Fa Zhou
9
Unions type SExp = | O | S of Sexp let rec sum x y = match x with
| O -> y | S x1 -> S(sum x1 y) by Neng-Fa Zhou
10
Unions (Cont.) type TreeInt = | Void | Leaf of int
| Node of int*TreeInt*TreeInt let rec count tree = match tree with | Void -> 0 | Leaf(_) -> 1 | Node(_,left,right) -> 1+ (count left) + (count right) by Neng-Fa Zhou
11
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
12
map and fold let rec map f lst = match lst with | [] -> []
| (car :: cdr) -> (f car)::(map f cdr) let rec fold f lst acc = | [] -> acc | (car :: cdr) -> f car (fold f cdr acc) let rec foldl f lst acc = | (car :: cdr) -> foldl f cdr (f car acc) by Neng-Fa Zhou
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.