1 / 20

Functional Programming and Scheme

Functional Programming and Scheme. Procedural vs. Functional. Procedural programs Focus on establishing a mutable state/referencing environment (that is, setting values to a bunch of variables). That mutable state creates an environment for all computations

Download Presentation

Functional Programming and Scheme

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 Programming and Scheme

  2. Procedural vs. Functional Procedural programs Focus on establishing a mutable state/referencing environment (that is, setting values to a bunch of variables). That mutable state creates an environment for all computations Complexity is handled by expansion of the mutable state Statements that Do Things are the main unit Functional programs Focus on definition and application of functions to carry out computations Complexity is handled by abstraction; defining and composing functions. Variables are much less important. Everything is a value, including functions. So functions are first-order values (first-class citizens Expressions (usually function applications) that Have Values are the main unit

  3. Procedural vs. Functional Procedural programs What we're used to Side effects everywhere Side effects are things your code does besides computing values (setting variables, printing things) Functional programs Ideally, no state, no side effects In practice, we minimize them Recursion is big here; many functional languages don't include loop constructs. Functions that take other functions as arguments common Code is often shorter Parallelizes well (without mutable state, the order in which things are executed is less important) These are more coding philosophies than anything – functional languages can have procedural aspects and vice versa. You can write procedural-feeling code in Scheme – this is where we will start

  4. From a theoretical standpoint • Functional programming is based on the lambda calculus

  5. Scheme • Computing values

  6. Scheme: Prefix notation • Function application works like in the lambda calculus (λx.x 2) Define f to be λx.x (f 2) + is a function, so (+ 2 3)

  7. Scheme: Prefix notation • Function application works like in the lambda calculus (λx.x 2) Define f to be λx.x (f 2) + is a function, so (+ 2 3) Technically ((+ 2) 3) Where + is λx. λy. y+x - that is λx. λy.[compute the sum of x and y] To make the parentheses slightly less insane, Scheme allows functions that take multiple arguments Note: Scala allows infix notation (related to the fact that it is object-oriented) TMI alert!

  8. Scheme – defining variables

  9. Scheme – defining variables • Define is a function! • Inputs a name and a value • Doesn’t give outputs – its purpose is to execute a side effect (augmenting the mutable state • Remember, most functional languages do allow side effects, we’re just going to try to minimize them

  10. Scheme – defining functions

  11. Scheme – defining functions • Works the same as defining variables – because functions are first-class citizens • It’s even the same function – it’s a polymorphic function • No explicit return needed; every expression has a value, which by extension gives the function its value

  12. Define a function plus1

  13. Scheme-conditionals

  14. Scheme-conditionals • If, cond are functions • Inputs: condition, and two pieces of code • Output: the result of running the selected piece of code – usually a value! • Cond is kind of like a switch statement or a nested else-if sequence • And, not, etc – all functions! • Your function names can be symbols! (define plus1 as ++)

  15. Example – trace execution • Activity

  16. Example – trace execution • But we’re defining all these functions – isn’t that mutable state? • Not really – because function definitions don’t change over time – they aren’t mutable!! • And variables aren’t mutable either! • Recursion • OK, so there is a loop construct in scheme, but don’t ever use it • To go with the loop there’s a mutable variable macro – again, don’t use it. • Both are just syntactic sugar for a tail-recursive function

  17. Write factorial in scheme

  18. Write factorial in scheme • Sometimes see simulated loops - internal iter functions • Don’t become too dependent on this kind of recursion!

  19. How we handle complexity in functional languages: Abstraction • 5-example • 6-funArgs

  20. Scope is actively managed! • Lexical scope • 7-let

More Related