Categories

# Functional Programming in Scala – Week 1

Started and completed this course in the second half of 2012. Thought revisiting the material and uploading the weekly assignments would be a good idea. Week 1 looked at basic functions and evaluations. The recursive nature of functional programming was alluded to, particularly in the assignment.

The basics of (x <- 0 to y) and other scala language specifics such as scoping and structure can all be reviewed in the weeks source code.

I signed up for this course after watching a presentation by Rich Hickey, the creator of Clojure (another functional language for the JVM).

http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey

weeks howework

Week 1 lecture videos: https://class.coursera.org/progfun-2012-001/lecture/8

Once of the most important concepts I took from week 1 was that of tail recursion:

```  /**
* Exercise 1
*/
def pascal(c: Int, r: Int): Int = {
//recursive function
def tFactorial(number: Int) : Int = {
//Calculate factorial with tail recursion
def tfactorialWithAccumulator(accumulator: Int, number: Int) : Int = {
if (number == 1) accumulator
else tfactorialWithAccumulator(accumulator * number, number - 1)
}
//start from the start!
tfactorialWithAccumulator(1, number)
}

// element value is calulated using r!/(c!(r-c)!)
if (c == 0 || c == r || r == 0 || r == 1) 1
else
tFactorial(r) / (tFactorial(c) * tFactorial(r-c))
}```