Skip to content

Commit 21cdc35

Browse files
committed
square roots
1 parent 4617c14 commit 21cdc35

11 files changed

+389
-389
lines changed

.gitignore

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
*.class
2-
*.log
3-
4-
# sbt specific
5-
dist/*
6-
target/
7-
lib_managed/
8-
src_managed/
9-
project/boot/
10-
project/plugins/project/
11-
12-
# Scala-IDE specific
13-
.scala_dependencies
14-
15-
# Eclipse
16-
.metadata
17-
1+
*.class
2+
*.log
3+
4+
# sbt specific
5+
dist/*
6+
target/
7+
lib_managed/
8+
src_managed/
9+
project/boot/
10+
project/plugins/project/
11+
12+
# Scala-IDE specific
13+
.scala_dependencies
14+
15+
# Eclipse
16+
.metadata
17+
1818
code/hw/*

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
functional-programming-in-scala
2-
===============================
3-
4-
https://class.coursera.org/progfun-003/class/index
1+
functional-programming-in-scala
2+
===============================
3+
4+
https://class.coursera.org/progfun-003/class/index

code/HelloWorld/.classpath

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
5-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
6-
<classpathentry kind="output" path="bin"/>
7-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

code/HelloWorld/.project

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<projectDescription>
3-
<name>HelloWorld</name>
4-
<comment></comment>
5-
<projects>
6-
</projects>
7-
<buildSpec>
8-
<buildCommand>
9-
<name>org.scala-ide.sdt.core.scalabuilder</name>
10-
<arguments>
11-
</arguments>
12-
</buildCommand>
13-
</buildSpec>
14-
<natures>
15-
<nature>org.scala-ide.sdt.core.scalanature</nature>
16-
<nature>org.eclipse.jdt.core.javanature</nature>
17-
</natures>
18-
</projectDescription>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>HelloWorld</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.scala-ide.sdt.core.scalabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.scala-ide.sdt.core.scalanature</nature>
16+
<nature>org.eclipse.jdt.core.javanature</nature>
17+
</natures>
18+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
eclipse.preferences.version=1
2-
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4-
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5-
org.eclipse.jdt.core.compiler.compliance=1.7
6-
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7-
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8-
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9-
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10-
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11-
org.eclipse.jdt.core.compiler.source=1.7
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package greeter
22

33
object WorkSheet {
4-
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
5-
val x = 5 //> x : Int = 5
6-
val i = x * x //> i : Int = 25
4+
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
5+
val x = 5 //> x : Int = 5
6+
val i = x * x //> i : Int = 25
77
}
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
package greeter
2-
3-
object Hello extends App {
4-
println("Hello, World!")
1+
package greeter
2+
3+
object Hello extends App {
4+
println("Hello, World!")
55
}
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package greeter
22

33
object WorkSheet {
4-
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
5-
val x = 5 //> x : Int = 5
6-
val i = x * x //> i : Int = 25
4+
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
5+
val x = 5 //> x : Int = 5
6+
val i = x * x //> i : Int = 25
77
}

notes/week 1/001-getting-started.md

+95-95
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,95 @@
1-
# Week One: Getting Started
2-
3-
##Programming Paradigms
4-
Three main programming paradigms:
5-
6-
* imperative programming
7-
* functional programming
8-
* a lesser known one called logic programming
9-
10-
Let's review what imperative programming is as a paradigm:
11-
12-
* modifying mutable variables
13-
* using assignments
14-
* and control structures such as if-then-else, loops, break, continue, and return
15-
16-
The most common informal way to understand imperative programs is as instruction sequences for a [Von Neumann computer](http://en.wikipedia.org/wiki/Von_Neumann_computer)
17-
18-
![http://upload.wikimedia.org/wikipedia/commons/e/e5/Von_Neumann_Architecture.svg](http://upload.wikimedia.org/wikipedia/commons/e/e5/Von_Neumann_Architecture.svg)
19-
20-
Consists of essentially a processor and memory, and a bus that reads both instructions and data from the memory into the processor.
21-
22-
What's important about this is that the width of that bus is about 1 [machine word](http://en.wikipedia.org/wiki/Word_(computer_architecture)), 32/64 bits.
23-
24-
It turns out that this model of a computer has shaped programming to no small degree. A strong correspondence between:
25-
26-
* mutable variables -> memory cells
27-
* variable dereferences -> load instructions
28-
* variable assignments -> store instructions
29-
* control instructions -> jumps
30-
31-
That's all very well - but the problem is scaling up. We want to avoid thinking about programs just word by word. We want to reason in larger structures...
32-
33-
##Scaling Up
34-
In the end, the pure imperative programming paradigm is limited by the "Von Neumann" bottleneck:
35-
>*One tends to conceptualize data structures word-by word.*
36-
37-
If want to scale up, we have to define higher level abstractions; collections, polynomials, geometric shapes, strings, documents...
38-
39-
Ideally, to be thorough, we need to develop *theories* of these higher level abstractions so that we are able to reason about them.
40-
41-
##What is a theory precious
42-
In mathematics, a theory consists of:
43-
44-
* one or more data types
45-
* operations on these types
46-
* laws that describe the relationships between values and operations
47-
48-
Here's what's important: *a theory in mathematics does not describe mutations.* IE, changing something while keeping the identity the same.
49-
50-
##Theories without Mutation
51-
For instance, the theory of polynomials defines the sum of two polynomials by laws such as
52-
>_(a*x + b) + (c*x + d) = (a+c)*x + (b+d)_
53-
54-
IE,. to sum two polynomials of degree 1 we take their two coefficients of the same degree and we sum those coefficients.
55-
56-
There would be laws of all the other useful operators for polynomials. But what the theory does *not* do is define an operator to change a coefficient while keeping the polynomial the same. Whereas if we look at imperative programming, one can do precisely that...
57-
58-
class Polynomial { double[] coefficient; }
59-
Polynomial p = ...;
60-
p.coefficient[0] = 42
61-
62-
The polynomial p is still the same, but we've changed it's coefficient. This isn't available in mathematics - it would detract from the theory and in fact could damage it by breaking laws
63-
64-
Another example - strings. Most programming languages have strings, and would define a concatenation operator. One of the laws of concatenation is that it is associative, such that
65-
>_(a ++ b) ++ c = a ++ (b ++ c)_
66-
67-
But it does not define an operator to change a sequence element while keeping the sequence the same
68-
69-
Some languages do get this right; ie, Java's strings are immutable; Java does not give you an operator to change a character in a string while keeping the string the same
70-
71-
##Consequences for Programming
72-
If we want to implement high-level concepts following their mathematical theories, there's no place for mutation
73-
74-
* the theories do not admit it
75-
* mutation can destroy useful laws in the theories
76-
77-
Therefore, let's:
78-
79-
* concentrate on defining theories for operators expressed as functions
80-
* avoid mutations
81-
* have powerful ways to abstract and compose functions
82-
83-
##Functional Programming
84-
In a *restricted* sense, functional programming means programming without mutable variables, assignments, loops, or other imperative control structures... It takes a lot of things away.
85-
86-
In a *wider* sense, FP means focusing on the functions. In particular, function can be values that are produced, consumed, and composed
87-
88-
Functional Programming languages can be viewed the same way - in a restricted sense, a functional programming language is one which does not have mutable variables, assignments, or imperative control structures.
89-
90-
In a wider sense, a FPL enables the construction of elegant programs that focus on functions. In particular, functions are first-class citizens, meaning essentially that we can do with a function what we could do with any other piece of data:
91-
92-
* they can be defined anywhere, including inside other functions; you can define a string anywhere, you should be able to define a function anywhere
93-
* like any other value, they can be passed as parameters to functions and returned as results
94-
* as for other values, there exists a set of operators to compose functions into richer functions
95-
1+
# Week One: Getting Started
2+
3+
##Programming Paradigms
4+
Three main programming paradigms:
5+
6+
* imperative programming
7+
* functional programming
8+
* a lesser known one called logic programming
9+
10+
Let's review what imperative programming is as a paradigm:
11+
12+
* modifying mutable variables
13+
* using assignments
14+
* and control structures such as if-then-else, loops, break, continue, and return
15+
16+
The most common informal way to understand imperative programs is as instruction sequences for a [Von Neumann computer](http://en.wikipedia.org/wiki/Von_Neumann_computer)
17+
18+
![http://upload.wikimedia.org/wikipedia/commons/e/e5/Von_Neumann_Architecture.svg](http://upload.wikimedia.org/wikipedia/commons/e/e5/Von_Neumann_Architecture.svg)
19+
20+
Consists of essentially a processor and memory, and a bus that reads both instructions and data from the memory into the processor.
21+
22+
What's important about this is that the width of that bus is about 1 [machine word](http://en.wikipedia.org/wiki/Word_(computer_architecture)), 32/64 bits.
23+
24+
It turns out that this model of a computer has shaped programming to no small degree. A strong correspondence between:
25+
26+
* mutable variables -> memory cells
27+
* variable dereferences -> load instructions
28+
* variable assignments -> store instructions
29+
* control instructions -> jumps
30+
31+
That's all very well - but the problem is scaling up. We want to avoid thinking about programs just word by word. We want to reason in larger structures...
32+
33+
##Scaling Up
34+
In the end, the pure imperative programming paradigm is limited by the "Von Neumann" bottleneck:
35+
>*One tends to conceptualize data structures word-by word.*
36+
37+
If want to scale up, we have to define higher level abstractions; collections, polynomials, geometric shapes, strings, documents...
38+
39+
Ideally, to be thorough, we need to develop *theories* of these higher level abstractions so that we are able to reason about them.
40+
41+
##What is a theory precious
42+
In mathematics, a theory consists of:
43+
44+
* one or more data types
45+
* operations on these types
46+
* laws that describe the relationships between values and operations
47+
48+
Here's what's important: *a theory in mathematics does not describe mutations.* IE, changing something while keeping the identity the same.
49+
50+
##Theories without Mutation
51+
For instance, the theory of polynomials defines the sum of two polynomials by laws such as
52+
>_(a*x + b) + (c*x + d) = (a+c)*x + (b+d)_
53+
54+
IE,. to sum two polynomials of degree 1 we take their two coefficients of the same degree and we sum those coefficients.
55+
56+
There would be laws of all the other useful operators for polynomials. But what the theory does *not* do is define an operator to change a coefficient while keeping the polynomial the same. Whereas if we look at imperative programming, one can do precisely that...
57+
58+
class Polynomial { double[] coefficient; }
59+
Polynomial p = ...;
60+
p.coefficient[0] = 42
61+
62+
The polynomial p is still the same, but we've changed it's coefficient. This isn't available in mathematics - it would detract from the theory and in fact could damage it by breaking laws
63+
64+
Another example - strings. Most programming languages have strings, and would define a concatenation operator. One of the laws of concatenation is that it is associative, such that
65+
>_(a ++ b) ++ c = a ++ (b ++ c)_
66+
67+
But it does not define an operator to change a sequence element while keeping the sequence the same
68+
69+
Some languages do get this right; ie, Java's strings are immutable; Java does not give you an operator to change a character in a string while keeping the string the same
70+
71+
##Consequences for Programming
72+
If we want to implement high-level concepts following their mathematical theories, there's no place for mutation
73+
74+
* the theories do not admit it
75+
* mutation can destroy useful laws in the theories
76+
77+
Therefore, let's:
78+
79+
* concentrate on defining theories for operators expressed as functions
80+
* avoid mutations
81+
* have powerful ways to abstract and compose functions
82+
83+
##Functional Programming
84+
In a *restricted* sense, functional programming means programming without mutable variables, assignments, loops, or other imperative control structures... It takes a lot of things away.
85+
86+
In a *wider* sense, FP means focusing on the functions. In particular, function can be values that are produced, consumed, and composed
87+
88+
Functional Programming languages can be viewed the same way - in a restricted sense, a functional programming language is one which does not have mutable variables, assignments, or imperative control structures.
89+
90+
In a wider sense, a FPL enables the construction of elegant programs that focus on functions. In particular, functions are first-class citizens, meaning essentially that we can do with a function what we could do with any other piece of data:
91+
92+
* they can be defined anywhere, including inside other functions; you can define a string anywhere, you should be able to define a function anywhere
93+
* like any other value, they can be passed as parameters to functions and returned as results
94+
* as for other values, there exists a set of operators to compose functions into richer functions
95+

0 commit comments

Comments
 (0)