functional programming languages

33 287 1
functional programming languages

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

    !"# COMS W4115 - PLT Columbia University 1 April 24, 2013 Functional Programming Languages and the Influence of Lambda Calculus Why Functional Programming? COMS W4115 - PLT Columbia University 2 April 24, 2013 You may have heard that: (1) functional programs are very succinct and elegant, but… (2) they take forever to write and understand $%& '% (')*+$, $ /)+$, $00 $%& '% (')*+$, $ /)+$, $00 Simple XOR program: Outline COMS W4115 - PLT Columbia University 3 April 24, 2013 I. What is Functional Programming? II. Lambda Calculus' Influence on Functional Programming III. Benefits of Functional Programming IV. OCaml in Action: An Entire Interpreter in 3 slides What is Functional Programming? COMS W4115 - PLT Columbia University 4 April 24, 2013 • A "programming paradigm" that focuses on the evaluation of functions • Programs are defined as recursive functions, where each takes a single input and outputs a single result. • Recall lambda calculus nests functions in this manner. Ex: (+ (* 1 2) (- 4 3)) *&  &1  !&-2 3!00 *&  &1  !&-2 3!00 In OCaml: What is Functional Programming? COMS W4115 - PLT Columbia University 5 April 24, 2013 • One important trademark of Functional Programming: It avoids both changes in state and mutable data These expressions are not equal. Conceptually, they are more like this. 4456 446 &0 &0 4456 446 &0 7 ),8 0 9 http://johnnycoder.com/blog/2009/04/20/functional-programming-part-1/ What is Functional Programming? COMS W4115 - PLT Columbia University 6 April 24, 2013 Some Common (more or less) FP Languages: • LISP • OCaml • Haskell • Erlang • F# (multi-paradigm) So what's the value in functional programming? Are its benefits worth taking the time to learn it and overcoming the (possibly) steep learning curve? Outline COMS W4115 - PLT Columbia University 7 April 24, 2013 I. What is Functional Programming? II. Lambda Calculus' Influence on Functional Programming III. Benefits of Functional Programming IV. OCaml in Action: An Entire Interpreter in 3 slides Lambda Calculus' Influence COMS W4115 - PLT Columbia University 8 April 24, 2013 • The syntax and theory of Functional Programming is highly influenced by lambda calculus. • Knowledge of lambda calculus means that FP is easier to learn, understand, and use efficiently. You might recognize some of the following FP syntax traits from lambda calculus… Lambda Calculus' Influence Function Abstraction COMS W4115 - PLT Columbia University 9 April 24, 2013 Lambda Calculus Recap A function abstraction consists of four parts: 1. a lambda followed by 2. a single variable, 3. a period, and then 4. an expression :$"$% Lambda Calculus' Influence Function Abstraction COMS W4115 - PLT Columbia University 10 April 24, 2013 :$"$% :$"$% Lambda Calculus: *$ $%00 *$ $%00 OCaml: *)$,&$% *)$,&$% Algebra: [...]... perform similar functionality to the "fun" command let x = expr2 in expr1;; let x = expr2 in expr1;; Example Usage: let x = 5 in x*x;; let x = 5 in x*x;; (* Evaluates to 5*5=25*) (* Evaluates to 5*5=25*) COMS W4115 - PLT Columbia University 15 April 24, 2013 Outline I II III IV What is Functional Programming? Lambda Calculus' Influence on Functional Programming Benefits of Functional Programming OCaml... – – – Memoization Parallelization Common Subexpression Elimination COMS W4115 - PLT Columbia University 23 April 24, 2013 Outline I II III IV What is Functional Programming? Lambda Calculus' Influence on Functional Programming Benefits of Functional Programming OCaml in Action: An Entire Interpreter in 3 slides COMS W4115 - PLT Columbia University 24 April 24, 2013 Implementing an Interpreter in OCaml... 2013 So Why Functional Programming? It's hard to get to compile, but once it compiles, it works -Unknown PLT Student COMS W4115 - PLT Columbia University 32 April 24, 2013 References • Edwards, Stephen “An Introduction to Objective Caml.” http://www.cs.columbia.edu/~sedwards/classes/2012/w4115-fall/ocaml.pdf • Huges, John “Why Functional Programming Matters.” Research Topics in Functional Programming, ... 16 April 24, 2013 Benefits of Functional Programming A few benefits in a nutshell: 1 2 3 4 Succinct Code Encourages disciplined and logical thinking Lazy Evaluation Higher Level Functions Example: List.fold_left (fun s e ->s + e) 0 [42; 17; 120];; 5 No Side Effects & Referential Transparency COMS W4115 - PLT Columbia University 17 April 24, 2013 Benefits of Functional Programming No Side Effects Examples:... http://www.cs.columbia.edu/~sedwards/classes/2012/w4115-fall/ocaml.pdf • Huges, John “Why Functional Programming Matters.” Research Topics in Functional Programming, Addison-Wesley, 1990: pp 17-42 • “Advantages of Functional Programming. ” http://c2.com/cgi/wiki? AdvantagesOfFunctionalProgramming COMS W4115 - PLT Columbia University 33 April 24, 2013 ... global/static variables Modify arguments Write data to a display or file interaction with the "outside world." Functional programs contain no assignment statements So variables, once given a value, never change COMS W4115 - PLT Columbia University 18 April 24, 2013 Benefits of Functional Programming No Side Effects Simple Side Effect Example in Java static int x = 5; static void changeX (int a){ x...Lambda Calculus' Influence Function Abstraction The Let Command • • Named Functions: OCaml strays from pure functional programming at times fun x -> expr;; The "let" command can be used to allow one to create named functions is equivalent to let myFunc x = expr;; COMS W4115 - PLT Columbia University 11 April 24,... static void changeX (int a){ x = a; } changeX(4); // This function has the side effect // of changing the value of global var x COMS W4115 - PLT Columbia University 19 April 24, 2013 Benefits of Functional Programming Referential Transparency Referential Transparency: An expression (e.g., function) is referentially transparent if it always produces the same output for the same input Because of this... the behavior of a program if that language has Referential Transparency Lack of side effects results in referential transparency COMS W4115 - PLT Columbia University 20 April 24, 2013 Benefits of Functional Programming Referential Transparency Lack of Referential Transparency in Java static int x = 10; static int x = 10; static int add(int a){ static int add(int a){ return a + x; return a + x; } } add(1); . 2013 Functional Programming Languages and the Influence of Lambda Calculus Why Functional Programming? COMS W4115 - PLT Columbia University 2 April 24, 2013 You may have heard that: (1) functional. University 3 April 24, 2013 I. What is Functional Programming? II. Lambda Calculus' Influence on Functional Programming III. Benefits of Functional Programming IV. OCaml in Action: An Entire. University 7 April 24, 2013 I. What is Functional Programming? II. Lambda Calculus' Influence on Functional Programming III. Benefits of Functional Programming IV. OCaml in Action: An Entire

Ngày đăng: 24/10/2014, 22:24

Từ khóa liên quan

Mục lục

  • Slide 1

  • Why Functional Programming?

  • Outline

  • What is Functional Programming?

  • What is Functional Programming?

  • What is Functional Programming?

  • Outline

  • Lambda Calculus' Influence

  • Outline

  • Benefits of Functional Programming

  • Benefits of Functional Programming No Side Effects

  • Benefits of Functional Programming No Side Effects

  • Benefits of Functional Programming Referential Transparency

  • Benefits of Functional Programming Referential Transparency

  • Why are Referential Transparency and Lack of Side Effects Good?

  • Why are Referential Transparency and Lack of Side Effects Good?

  • Outline

  • Implementing an Interpreter in OCaml

  • Implementing an Interpreter in OCaml

  • Implementing an Interpreter in OCaml

Tài liệu cùng người dùng

Tài liệu liên quan