Functional Programming Principles: Difference between revisions
Adamcolton (talk | contribs) |
Adamcolton (talk | contribs) |
||
| (10 intermediate revisions by the same user not shown) | |||
| Line 17: | Line 17: | ||
call-by-name vs call-by-value. | call-by-name vs call-by-value. | ||
CBV evaluates all values first, CBN does substitution first. | CBV evaluates all values first, CBN does substitution first. | ||
From the next lesson, a good example | |||
def and(x:Boolean, y: =>Boolean): if (x) y else false | |||
Now if we call and(false, loop) where loop is an infinite loop, it will not evaluate the second argument. If we were to write it as | |||
def and(x:Boolean, y:Boolean): if (x) y else false | |||
then it would try to evaluate the second arg and would loop forever. | |||
==== Evaluation Strategies and Termination ==== | ==== Evaluation Strategies and Termination ==== | ||
| Line 30: | Line 36: | ||
but it's more like ternary. | but it's more like ternary. | ||
==== Blocks and Lexical Scope ==== | ==== Blocks and Lexical Scope ==== | ||
A block is delimited by braces. It returns any statement it evaluates. | |||
==== Tail Recursion ==== | ==== Tail Recursion ==== | ||
If a function calls itself as its last action, the function's stack frame can be reused. This is tail recursion. | |||
=== Week 2: Higher Order Functions === | |||
==== Higher-Order Function ==== | |||
Functions that accept other functions are higher order functions. Anonymous functions (function literals). | |||
==== Currying ==== | |||
Used as function builders. | |||
In Scala, this is supported out-of-the-box: | |||
f(a)(b,c) | |||
This automatically allows us to curry a. | |||
==== Example: Finding Fixed Points ==== | |||
Shows finding square root with successive approximations and average dampening. | |||
==== Scala Syntax Summary ==== | |||
==== Functions and Data ==== | |||
talking about Scala classes. | |||
==== More Fun with Rationals ==== | |||
Added GCD to rational class. | |||
Talked about data abstraction. | |||
==== Evaluation and Operators ==== | |||
=== Types and Pattern Matching === | |||
==== Functions as Objects ==== | |||
functions are objects. eta-expansion | |||
==== Subtyping and Generics ==== | |||
==== Objects Everywhere ==== | |||
==== Variance ==== | |||
==== Decomposition ==== | |||
==== Pattern Matching ==== | |||
==== Lists ==== | |||
== Footer == | == Footer == | ||
[[Category:Coursework]] | [[Category:Coursework]] | ||
[[Category:Notes]] | [[Category:Notes]] | ||
Latest revision as of 02:09, 19 November 2014
Video Lectors
Getting Started
Tutorial: Working on Assignments
Using Eclipse and SBT
Working from scala repl
InelliJ IDEA
Week 1: Functions & Evaluations
Programming Paradigms
Programming languages are tightly coupled with the Von Neumann architecture. Functional programming is a way to decouple this dependency. It's also good for scalable computing.
Elements of Programming
SBT: scala build tool
call-by-name vs call-by-value. CBV evaluates all values first, CBN does substitution first.
From the next lesson, a good example
def and(x:Boolean, y: =>Boolean): if (x) y else false
Now if we call and(false, loop) where loop is an infinite loop, it will not evaluate the second argument. If we were to write it as
def and(x:Boolean, y:Boolean): if (x) y else false
then it would try to evaluate the second arg and would loop forever.
Evaluation Strategies and Termination
CBV termination → CBN termination but CBN termination !→ CBV termination
Scala uses CBV but you can force CBN with =>
Conditionals and Value Definitions
if (a) b else c
but it's more like ternary.
Blocks and Lexical Scope
A block is delimited by braces. It returns any statement it evaluates.
Tail Recursion
If a function calls itself as its last action, the function's stack frame can be reused. This is tail recursion.
Week 2: Higher Order Functions
Higher-Order Function
Functions that accept other functions are higher order functions. Anonymous functions (function literals).
Currying
Used as function builders.
In Scala, this is supported out-of-the-box:
f(a)(b,c)
This automatically allows us to curry a.
Example: Finding Fixed Points
Shows finding square root with successive approximations and average dampening.
Scala Syntax Summary
Functions and Data
talking about Scala classes.
More Fun with Rationals
Added GCD to rational class.
Talked about data abstraction.
Evaluation and Operators
Types and Pattern Matching
Functions as Objects
functions are objects. eta-expansion