Functional Programming

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes.
F# Overview: Immutable Data + Pure Functions. Acknowledgements Authored by – Thomas Ball, MSR Redmond Includes content from the F# team.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Type checking © Marcelo d’Amorim 2010.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
By Neng-Fa Zhou Functional Programming 4 Theoretical foundation –Church’s -calculus expressions and evaluation rules 4 Characteristics –Single assignment.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.
Presented by Neng-Fa Zhou1 Evolution of programming languages –Machine language –Assembly language –Sub-routines and loop (Fortran) –Procedures and recursion.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
By Neng-Fa Zhou1 Evolution of programming languages –Machine language –Assembly language –Sub-routines and loop (Fortran) –Procedures and recursion (Algol,
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Overview of the Haskell 98 Programming Language
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Language Oriented Programming in F# Robert Pickering, Chance Coble, Roger Castillo.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Solution of Mid. Term 2009 Consider the following C++ declarations and assignments. int i, j, k ; float x, y ; char c ; bool test ; i = 35 ; j= 5 ; k =
More Data Types CSCE 314 Spring CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for.
An Introduction to F# Sushant aboutdev.com
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
CS314 – Section 5 Recitation 9
Programming Language History and Evolution
Functional Programming
Programming Languages and Compilers (CS 421)
CS 550 Programming Languages Jeremy Johnson
Principles of programming languages 12: Functional programming
Programming Languages
dr Robert Kowalczyk WMiI UŁ
Basic 1960s It was designed to emphasize ease of use. Became widespread on microcomputers It is relatively simple. Will make it easier for people with.
ML: a quasi-functional language with strong typing
Chapter 11 :: Functional Languages
A lightening tour in 45 minutes
More on Lambda Calculus
CSE 3302 Programming Languages
Evolution of programming languages
Main Points of Haskell Functional programming Laziness
Programming Languages
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
FP Foundations, Scheme In Text: Chapter 14.
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
Announcements Quiz 5 HW6 due October 23
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
Programming Languages and Compilers (CS 421)
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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 | [] -> [] | (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