1 / 37

Functional Programing

Functional Programing. Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ ajbalaam ). Historical Origins. How did we get here. History.

saxon
Download Presentation

Functional Programing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)

  2. Historical Origins How did we get here

  3. History From the work of Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, and others Each worked on their own Each made a formalized notion of an algorithm Church’s Thesis: Any intuitively appealing model of computing would be equally powerfull

  4. Two Paradigms Turing Machine (Imperative Languages) Lambda Calculus (Functional Languages) Based on parameterized expressions, each parameter is introduced with a One substitutes parameters into expression to compute each expression Example: Scheme (later) Lisp (scheme, rocket), Haskell, Miranda, pH, Sisal, Single Assignment C, Erlang • Based on pushdown automaton • A pushdown automata uses a stack • Uses an unbounded storage “tape” • Computation is done by reading and writing values from cells on the tape • Example: Google Doodle for Alan Turing’s 100thBirthday • All of the languages you have learned in 201 and 202

  5. Functional Programming Concepts A completely new paradigm

  6. No side effects Based on function A function takes parameters and returns something Functions can not modify values

  7. First Class values Everything is a first class value, including functions This allows for higher order functions, which operate on functions.

  8. Polymorphism • Most functional languages are polymorphic • Lisp (Scheme, Rocket, etc.) is dynamically typed • Functions can take many different types and conditionally deal with them based on type

  9. Lists A list is an item followed by a list This leads to natural recursion Provides the only way to repeatedly do something Operate on the first element, do the same with the rest (hint: recursion)

  10. Scheme A language with only one feature

  11. Scheme is a dialect of Lisp Lisp stands for LIStProcessing It is usually interpreted, although can be compiled Scheme uses prefix (Caimbrige Polish Notation) – although this makes sense

  12. Scheme Interpreters • Dr. Scheme – deprecated • Rocket – for the Rocket dialect • MIT Scheme – its own implementation My Chosen best: SISC - Second Interpreter of Scheme Code • In java – portable • Uses standard Scheme in a simple command-line environment

  13. You can do one thing (item itemitemitemitemitem item)

  14. A Scheme program (operation operationoperation operation) Operation: (operator operand operand operand)

  15. How to do things Addition, subtraction, multiplication, and division are predefined and referred to with +,-,*,/. Other operations, like modulus are referred to with words In order to trigger evaluation you must wrap an operation in parenthasys (+ 1 2) evaluates to 3 7 is already evaluated, it results in 7 (7) tries to run the function 7. 7 is not a function Similarly ((+ 1 2)) tries to run the function 3

  16. How to not do things A single quote defines a list Because an operation is a list, this means that we can use the single quote to do operations on a operation or return the operation ‘(+ 1 2) results in the list (+ 1 2)

  17. Booleans #t for true #f for false

  18. Control flow If Cond Cond ([boolean] [expr]) ([boolean] [expr]) (else [expr if else]) If [Boolean] [expr if true] [expr if false]

  19. Dynamic typing (if (> a 0) (+ 2 3) (+ 2 “foo”)) This will execute fine? Why?

  20. Defining items – lambda expressions From lambda calculus Lambda takes two arguments, a list of identifiers, and an expression to compute using them Lambda (x) (* x x) is a function that takes a value and returns its square

  21. Defining – function ‘Define’As the Book does it Define takes two parameters, an identifier, and a function (Define pow (lambda (x) (* x x)) allows us to use the function pow that takes a parameter and returns its square

  22. Defining – function ‘Define’Another way Define takes two parameters, a list matching how it should be called, and an expression using the identifiers given in the first part This merges lambda expressions and definition (define (pow x) (* x x)) defines the same thing as before

  23. Defining – local bindings Defining is just global binding You can create local bindings using let Let takes a list of defines parameters and an expression, and runs the expression using that set of defines

  24. Lists Everything is a list Recall: a single quote makes a set of parenthesis not evaluate and stay as a list ‘(1 2 3) is a list Recall: a list is an item followed by a list What about the last item? Null? [list]

  25. List operations Car [list] Cdr [list] Cons [item] [list]

  26. Higher-Order Functions I heard you like functions, so we made your functions return functions, so you can compute what you compute.

  27. Metaprogramming is just programming Metaprogramming is writing code about code Lisp doesn’t care Lisp is homoiconic – a lisp program is a list. A function can be an argument to a function, or it can be returned from a function

  28. Common Higher order functions Define Load Lambda For-each Call apply compose

  29. Example – Folding (define fold (lambda (f I l) (if (null? L) I (f (car l) (fold f I (cdr l)))))) This takes a function f to fold the list l using the identity i

  30. Evaluation Order Putting functional programming in order

  31. Evaluation order Applicative-order Normal-order You pass each argument as an unevaluated expression • You evaluate each argument before you pass it to a function

  32. Example (Right from the book) (Define double (lambda (x) (+ x x))) (double (* 3 4)) Applicative-order Normal-order (double (* 3 4)) (+ (* 3 4) (* 3 4)) (+ 12 (* 3 4)) (+ 12 12) 24 This is much longer We calculate the same value twice (double (* 3 4)) (double 12) (+ 12 12) 24 What kind of cases could applicative order be wasteful?

  33. Scheme The book claims that scheme evaluates in applicative-order. But what about this line? (We just saw this in dynamic typing) (if (> a 0) (+ 2 3) (+ 2 “foo”))

  34. In reality: Lazy evaluation We evaluate any evaluable expressions and store their value for later use We can forget about this, it is behind the scenes

  35. Functional Programming in Perspective Functional and comparative, when and why

  36. Side-effect free • Its simple • Not much advanced computer science needed • Perfect for math • No required evaluation order (other than common sense) • Parallelism doesn’t matter (the only way to “talk” is to pass variables) • Some general programming ideas require assignment (we can’t do that) • I/O is difficult (technically impossible without side effects) • Any small update requires an entire new copy of the data

  37. In conclusion Fun Easy to use You can make a computational program easily Not a tool for every job, but every tool has a job.

More Related