7. What is the typing model?
What is the programming model?
How will you interact with it?
What are the decision constructs
and core data structures?
What are the core features that
make the language unique?
8. Java was like having a rich lawyer as a brother. He was
fun when he was younger, but now he’s a black hole
that sucks away all the joy in a 100-mile radius.
14. Io is a rule bender. He’s young, wicked smart, and easy to
understand but hard to predict. He might give you the ride of
your life, wreck your dad’s car, or both.
16. Objects and Slots
Io> Person := Object clone
==> Person_0x7f922c06ad00:
type = "Person"
Io> Person firstName := "John"
==> John
Io> Person lastName := "Doe"
==> Doe
Io> Person firstName
==> John
23. Actors
Io> Slow := Object clone
Io> Fast := Object clone
Io> Slow go := method(
wait(1)
"Slow" println
)
Io> Fast go := method(
"Fast" println
)
Io> Slow @@go; Fast @@go; wait(2)
Fast
Slow
24. Futures
Io> page := URL with(
"http://iprug.org/") @fetch
Io> # do other things while waiting
Io> page size println
# blocks until ready
91559
25. Pros
Tiny – ideal for embedded systems
Prototypes and duck typing are very flexible
Concurrency support
Simple, consistent syntax
Raw execution speed
Small user community
Cons
36. Pros
Solving logical and scheduling problems
Natural language processing
Artificial intelligence
Scaling requires deep understanding
Not a general-purpose language
Simple procedural tasks are difficult
Cons
38. He was often awkward, was sometimes amazing, but always had a
unique expression. Sometimes, his scissors let him do incredible things.
Other times, he was awkward and humiliated.
39. Type inference & coercion
scala> "foo"
res0: java.lang.String = foo
scala> 123
res1: Int = 123
scala> 45.6
res2: Double = 45.6
scala> 1 + 2.3
res3: Double = 3.3
scala> "The answer is " + 42
res4: java.lang.String = The answer is 42
40. var and val
scala> var a = 42
a: Int = 42
scala> a = 69
a: Int = 69
scala> val b = 999
b: Int = 999
scala> b = 911
<console>:8: error: reassignment to val
b = 911
^
41. Ranges and tuples
scala> val range = 0 to 3
range: scala.collection.immutable.Range.Inclusive
= Range(0, 1, 2, 3)
scala> val range = 0 to 5 by 2
range: scala.collection.immutable.Range
= Range(0, 2, 4)
scala> val tuple = ("Kerry", 42)
tuple: (java.lang.String, Int) = (Kerry,42)
scala> val (name, age) = tuple
name: java.lang.String = Kerry
age: Int = 42
42. Defining classes
class Person(firstName: String,
lastName: String) {
def greet(name: String) {
println("Hello " + name +
", I'm " + firstName + ".")
}
}
scala> val kerry = new Person("Kerry",
"Buckley")
kerry: Person = Person@276bab54
scala> kerry.greet("IPRUG")
Hello IPRUG, I'm Kerry.
43. Objects & Class Methods
object Highlander {
def copy {
println("There can be only one!")
}
}
scala> Highlander.copy
There can be only one!
44. Extending & traits
class Person(val name:String)
trait Nice {
def greet() = println("Howdily doodily.")
}
class Character(override val name:String)
extends Person(name) with Nice
scala> val flanders = new Character("Ned")
scala> flanders.greet
Howdily doodily.
52. Concurrency with actors
case object Stroke
case object Feed
class Cat() extends Actor {
def act() {
loop {
react {
case Stroke => { println("Purr!") }
case Feed => { println("Om nom nom") }
}
}
}
}
scala> val cat = new Cat().start
scala> cat ! Stroke; cat ! Feed; println("Done.")
Done.
Purr!
Om nom nom
53. Pros
A modern version of Java
Mixins, pattern matching, blocks, XML
Concurrency with actors & immutability
Static typing
Compromises with mutable state
Cons
55. Agent Smith was an artificial intelligence program in the matrix that
had an amazing ability to take any form and bend the rules of
reality to be in many places at once. He was unavoidable.
57. Variables don’t change
1> Foo = 123.
123
2> Foo.
123
3> Bar = 456.
456
4> Foo + Bar.
579
5> Foo = 1.
** exception error: no match of right hand side value 1
61. Patterns in functions
-module(fact).
-export([fact/1]).
fact(0) -> 1;
fact(N) -> N * fact(N-1).
1> c(fact).
{ok,fact}
3> fact:fact(6).
720
4> fact:fact("foo").
** exception error: bad argument in an arithmetic
expression
in function fact:fact/1 (fact.erl, line 5)
6> fact:fact(10000).
65. Control structures
1> List = [1, 2, 3].
[1,2,3]
2> case List of
2> [] -> none;
2> [_|[]] -> one;
2> _ -> some
2> end.
some
3> A = 42.
42
4> if
4> A < 10 -> small;
4> A < 100 -> big;
4> true -> massive
4> end.
big
66. Higher order functions
1> Double = fun(X) -> X * 2 end.
#Fun<erl_eval.6.111823515>
2> Double(2).
4
3> List = [1, 2, 3].
[1,2,3]
4> lists:map(Double, List).
[2,4,6]
5> lists:map(fun(X) -> X + 1 end, List).
[2,3,4]
6> lists:foldl(fun(X, Sum) -> X + Sum end, 0, List).
6
73. Pros
Designed for concurrency & fault tolerance
Flexibility of dynamic typing
Lightweight processes with message passing & monitoring
Build scalable applications with the OTP library
Syntax sometimes a little clumsy
Integration with other languages
Cons
75. His communication style is often inverted and hard to understand.
He seems too small to make a difference, but it quickly becomes
apparent that there is more to Yoda than meets the eye.
99. Agents in the background
user=> (defn slow-twice [x]
(do (Thread/sleep 5000) (* 2 x)))
#'user/slow-twice
user=> (def number (agent 2))
#'user/number
user=> number
#<Agent@4c825cf3: 2>
user=> @number
2
user=> (send number slow-twice)
#<Agent@4c825cf3: 2>
user=> @number
2
user=> @number
4
100. Back to the futures
user=> (def ultimate-answer
(future (do
(Thread/sleep 2.4e17)
42)))
#'user/ultimate-answer
user=> @ultimate-answer
42
101. Pros
A good lisp implementation
Access to Java ecosystem
Concurrency provided by STM
Prefix notation can be confusing
(((((and all those parens don’t help)))))
Some limitations compared to other lisps
Cons
103. Haskell represents purity and freedom, but the power comes at a
price. Think Spock from Star Trek. His character has a single-minded
purity that has endeared him to generations.
105. Type inference
Prelude> :set +t
Prelude> 42
42
it :: Integer
Prelude> "Hello"
"Hello"
it :: [Char]
Prelude> 2 == 2
True
it :: Bool
Prelude> 1/2
0.5
it :: Double
106. Defining functions
Prelude> let double x = x * 2
double :: Num a => a -> a
Prelude> double 4
8
Prelude> let times x y = x * y
times :: Num a => a -> a -> a
Prelude> times 5 6
30
107. Specifying function types
module Main where
double :: Integer -> Integer
double x = 2 * x
Prelude> :load main
[1 of 1] Compiling Main ( main.hs, interpreted )
Ok, modules loaded: Main.
*Main> double 2
4
108. Pattern matching & guards
module Main where
factorial :: Integer -> Integer
factorial 0 = 1
factorial x = x * factorial (x - 1)
factorial2 :: Integer -> Integer
factorial2 x
| x > 1 = x * factorial2 (x - 1)
| otherwise = 1
109. Tuples & lists
Prelude> let (x, y) = (1, 2)
Prelude> x
1
Prelude> y
2
Prelude> let (head:tail) = [1, 2, 3, 4]
Prelude> head
1
Prelude> tail
[2,3,4]
Prelude> 1:[2, 3]
[1,2,3]
Prelude> [1, "two"]
<interactive>:1:2:
No instance for (Num [Char])
arising from the literal `1'
111. List comprehensions
Prelude> let numbers = [1,2,3]
Prelude> [x * 2 | x <- [1, 2, 3]]
[2,4,6]
Prelude> [[x, y] | x <- numbers, y <- numbers]
[[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]]
Prelude> [[x, y] | x <- numbers, y <- numbers, x < y]
[[1,2],[1,3],[2,3]]
Prelude> let more_numbers = [4,5]
Prelude> [(x, y, x * y) | x <- numbers, y <- more_numbers, x < y]
[(1,4,4),(1,5,5),(2,4,8),(2,5,10),(3,4,12),(3,5,15)]
112. Function currying
Prelude> 4 * 5
20
Prelude> (*) 4 5
20
Prelude> :t (*)
(*) :: Num a => a -> a -> a
Prelude> let double = (* 2)
Prelude> :t double
double :: Integer -> Integer
Prelude> double 3
6
114. User-defined types
module Main where
data Tree a = Children [Tree a] | Leaf a
deriving (Show)
depth (Leaf _) = 1
depth (Children c) = 1 + maximum (map depth c)
*Main> let tree = Children[Leaf 1, Children [Leaf 2, Leaf 3]]
*Main> let x(Children [left, right]) = tree
*Main> left
Leaf 1
*Main> right
Children [Leaf 2,Leaf 3]
*Main> depth left
1
*Main> depth right
2
115. Type classes
class Eq a where
(==), (/=) :: a -> a -> Bool
-- Minimal complete definition:
-- x/=y x==y
(==) or (/=)
= not(x==y) = not(x/=y)
*Main> :info Integer
data Integer
= integer-gmp:GHC.Integer.Type.S# GHC.Prim.Int#
| integer-gmp:GHC.Integer.Type.J# GHC.Prim.Int#
GHC.Prim.ByteArray#
-- Defined in integer-gmp:GHC.Integer.Type
instance Enum Integer -- Defined in GHC.Num
instance Eq Integer -- Defined in GHC.Classes
instance Integral Integer -- Defined in GHC.Real
instance Num Integer -- Defined in GHC.Num
instance Ord Integer -- Defined in GHC.Classes
instance Read Integer -- Defined in GHC.Read
instance Real Integer -- Defined in GHC.Real
instance Show Integer -- Defined in GHC.Num
116. Monads
A monad is a construction that, given an underlying type
system, embeds a corresponding type system (called
the monadic type system) into it (that is, each monadic
type acts as the underlying type). This monadic type
system preserves all significant aspects of the
underlying type system, while adding features particular
to the monad.
Monads must obey the following rules:
• (return x) >>= f ≡ f x
• m >>= return ≡ m
• (m >>= f) >>= g ≡ m >>= ( x -> (f x >>= g) )
117.
118. The maybe monad
data Maybe a = Nothing | Just a
Prelude> case (html doc) of
Nothing -> Nothing
Just x -> case body x of
Nothing -> Nothing
Just y -> paragraph 2 y
instance Monad Maybe where
return = Just
Nothing >>= f = Nothing
(Just x) >>= f = f x
Prelude> Just someWebPage >>= html >>= body
>>= paragraph >>= return
119. Pros
Pure functional language with no side effects
Strong typing with powerful type inference
Laziness reduces need for recursion
Ideal for learning the functional paradigm
Inflexibility and lack of compromise
Small community outside academia
Steep learning curve (especially monads!)
Cons
121. Functional style
def total_price(widgets)
total = 0
widgets.each do |widget|
if widget.red?
total += widget.price
end
end
total
end
def total_price(widgets)
widgets.select{|w| w.red?}.map{|w| w.price}.reduce(&:+)
end
122. JVM or standalone?
• Access to Java libraries
• Deployment environments
• Escape route for Java developers
• Limitations (eg tail recursion)
123. Know what’s out there
• Languages
• Features
• First-class functions
• List comprehensions
• Pattern matching
• Lazy evaluation