200 likes | 327 Views
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
E N D
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
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
From a theoretical standpoint • Functional programming is based on the lambda calculus
Scheme • Computing values
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)
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!
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
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
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 ++)
Example – trace execution • Activity
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
Write factorial in scheme • Sometimes see simulated loops - internal iter functions • Don’t become too dependent on this kind of recursion!
How we handle complexity in functional languages: Abstraction • 5-example • 6-funArgs
Scope is actively managed! • Lexical scope • 7-let