Skip to content

Instantly share code, notes, and snippets.

@qcam
Last active Oct 16, 2016
Embed
What would you like to do?
My note for Functional Programming Principles in Scala course on Coursera - https://www.coursera.org/learn/progfun1/

Functional Programming

Week 1

Programming Paradigms

Main programming paradigms include:

Imperative Programming
  • Modifying mutable variables.
  • Using assignments and control structures e.g. loops, goto, etc.
Functional Programming

is a way of programming without:

  • Mutations
  • Assignments
  • Loops
  • or other imperative control structure
  • and focus on functions

In FP, functions are first-class citizen, which means:

  • They can be defined anywhere, even in other functions.
  • They can be passed as an argument to other functions.
  • They can be returned as result of a function.
Logic Programming

Programming elements

call-by-name and call-by-value

call-by-value has the advantage that it only evaluates every function argument once.

call-by-name has the advantage that a function argument is not evaluated if it is unused in the function body.

call-by-name vs lazy-evaluation

a lazy-evaluated expression is an expression evaluated only once when it's requested, and caches the result.

call-by-name is an evaluation strategy for function arguments, function arguments will only be evaluated if it's in use in the function body.

If an expression terminates in CBV, it terminates in CBN too. The reversed isn't true.

// Example expression that terminates in CBN but doesn't in CBV
def loop: Int = if (true) loop else 1

def callByValue(a: Int, b: Int): Int = if (a > 0) a else b

def callByName(a: Int, b: => Int): Int = if (a > 0) a else b

callByValue(1, loop) // this doesn't terminate
callByName(1, loop) // this terminates
Tail Recursion

If a function call itself as its last action, the stack-frame can be reused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment