9. John McCarthy
1927 - 2011
Lisp, 1958
(DEFUN FAC (N)
(IF (= N 0)
1
(* N (FAC (- N 1)))))
• Value-oriented
• Garbage collection
• Lambda functions
10. Guy Steele
1954-
Scheme, 1970
(define (fac n)
(if (= n 0)
1
(* n (fac (- n 1))))))
• Lexical scope
• Tail call optimization
• Continuations
Gerald Sussman
1947-
11. Robin Milner, et al
1934 - 2010
ML, 1973
fun fac (0 : int) : int = 1 |
fac (n : int) : int =
n * fac (n - 1)
• Statically typed
• HM type inference
• ADTs, Pattern matching
12. Haskell Committee
Haskell, 1990
fac :: (Integral a) => a -> a
fac n = if n < 2
then 1
else n * fac (n-1)
• Purity
• Lazy evaluation
• Monads
34. Referential Transparency
“An expression is said to be
referentially transparent if it can be
replaced with its corresponding value
without changing the program's
behavior.”
- Wikipedia
35. It’s just good sense!
Being able to reason about software is
just good programming!
38. case class Point(
var x: Int,
var y: Int)
• Mutable
• Instances are identities that track a
series of Point states over time, where
you only get to see the latest one
• Not simple
39. "Classes should be immutable unless there's a
very good reason to make them mutable....If a
class cannot be made immutable, limit its
mutability as much as possible."
- Joshua Bloch
Effective Java
41. Entities vs value types
“Some objects are not defined primarily by
their attributes. They represent a thread of
identity that runs through time and often
across distinct representations.”
- Eric Evans
Domain Driven Design
42. Entities vs value types
samuel_l_jackson.hairstyle
samuel_l_jackson.occupation
43. Entities vs value types
samuel_l_jackson.hairstyle
samuel_l_jackson.occupation
=> “bald”
=> “actor”
44. Entities vs value types
samuel_l_jackson.hairstyle
samuel_l_jackson.occupation
=> “bald”
=> “actor”
in 2017…
45. Entities vs value types
samuel_l_jackson.hairstyle
samuel_l_jackson.occupation
=> “afro”
=> “student”
1960s:
54. Sum types (Scala)
sealed trait Maybe[+A]
case class Just[A](a: A)
extends Maybe[A]
case object None
extends Maybe[Nothing]
55. Sum types (Java)
public class Maybe<A> {
private Maybe() {}
public abstract <B> B fold(Function<A,B> ifPresent, Supplier<B> ifAbsent);
public static class Just<A> extends Maybe<A> {
private final A a;
public Just(A a) {
this.a = a;
}
public <B> B fold(Function<A,B> ifPresent, Supplier<B> ifAbsent) {
ifPresent.apply(a);
}
}
public static class Nothing<A> extends Maybe<A> {
public <B> B fold(Function<A,B> ifPresent, Supplier<B> ifAbsent) {
ifAbsent.get();
}
}
}
62. What can we know from a type?
process :: a
-> (a -> Bool)
-> WriterT Log (Reader Config) a
• Takes a predicate that operates only on the given item
• We do nothing with the “a” value other than filter it and return it
• No side effects are performed
• We might write an accumulating “Log” value
• The code declares what will take place, but does not execute it when called
• A “Config” will be required before the code can be eventually executed
65. (define (sum-primes a b)
(define (iter count accum)
(cond ((> count b) accum)
((prime? count)
(iter (+ count 1)
(+ count accum)))
(else (iter (+ count 1) accum))))
(iter a 0))
Sum primes, iteratively
from SICP
66. (define (sum-primes a b)
(define (iter count accum)
(cond ((> count b) accum)
((prime? count)
(iter (+ count 1)
(+ count accum)))
(else (iter (+ count 1) accum))))
(iter a 0))
Sum primes, iteratively
Test each number to see if
it is a prime
67. (define (sum-primes a b)
(define (iter count accum)
(cond ((> count b) accum)
((prime? count)
(iter (+ count 1)
(+ count accum)))
(else (iter (+ count 1) accum))))
(iter a 0))
Sum primes, iteratively
Accumulate total sum
68. (define (sum-primes a b)
(define (iter count accum)
(cond ((> count b) accum)
((prime? count)
(iter (+ count 1)
(+ count accum)))
(else (iter (+ count 1) accum))))
(iter a 0))
Sum primes, iteratively
Efficiency 👍
• The number range is only
traversed once
Modularity 👎
• Prime test and summation all
smooshed together in loop
69. (define (sum-primes a b)
(accumulate +
0
(filter prime?
(enumerate-interval a b))))
Sum primes, with list ops
Efficiency 👎
• The number list is traversed
several times
Modularity 👍
• Prime test impl & summation
impl just plug in together
70. (define (sum-primes a b)
(accumulate +
0
(stream-filter prime?
(enumerate-stream-interval
a b))))
Sum primes, with lazy stream ops
Modularity 👍
• Prime test impl & summation
impl just plug in together
Efficiency 👍
• The number range is only
traversed once
71. “Lazy evaluation is perhaps the most
powerful tool
for modularization in the functional
programmer’s repertoire..”
- John Hughes
Why Functional
Programming Matters
72. Living the dream
Imagine a world where…
You don’t have to chase down
runtime stack traces in the logs,
because they can’t happen
74. Living the dream
Imagine a world where…
You can write software by clicking
typed functions together like Lego
75. Living the dream
Imagine a world where…
You can reason about one thing at
a time without cramming the
whole world in
76. User groups
Melbourne Functional User Group
https://www.meetup.com/en-AU/Melbourne-Functional-User-
Group-MFUG/
Melbourne Scala User Group
https://www.meetup.com/en-AU/Melbourne-Scala-User-Group/
Melbourne Haskell Users Group
https://www.meetup.com/en-AU/Melbourne-Haskell-Users-Group/
clj-melb
https://www.meetup.com/en-AU/clj-melb/
Notes de l'éditeur
Powerful source of good software ideas
Lambda functions
Immutability
Tuples
Data classes
Pattern matching
Garbage Collection