Five Languages
   in a Moment




        Sergio Gil
Sergio Gil Pérez de la Manga




            @porras
I   Creating Software
I   Ruby
“Excusatio non petita,
accussatio manifesta”
2005
2005
“Learn a new language each year”
2006
2006   Ruby
2006   Ruby


2007
2006   Ruby


2007    -
2006   Ruby


2007    -


2008
2006   Ruby


2007    -


2008    -
2006   Ruby


2007    -


2008    -


2009
2006   Ruby


2007    -


2008    -


2009    -
2006   Ruby


2007    -


2008    -


2009    -


2010
2006   Ruby


2007    -


2008    -


2009    -


2010    -
Ruby is
awesome!!
2010
THE
   POWER
CONTINUUM
Highest level




Lowest level
Highest level




                Machine
Lowest level
Highest level




                Assembler

                Machine
Lowest level
Highest level




                   C


                Assembler

                Machine
Lowest level
Highest level




                  Java


                   C


                Assembler

                Machine
Lowest level
Highest level



                  Ruby


                  Java


                   C


                Assembler

                Machine
Lowest level
Abstractions are tools for the mind
1
Created by Joe Armstrong at Ericsson in 1986
Functional Language
Functional Language



Concurrency Oriented
Functional Language



Concurrency Oriented



Distribution Oriented
Functional Language



Concurrency Oriented



Distribution Oriented



   Error Tolerant
Functional Programming
http://www.defmacro.org/ramblings/fp.html
Side effects free programming
x = x + 1
> X = 1.
> X = 1.
> X.
1
> X = 1.
> X.
1
> X = 2.
** exception error
Pattern Matching
> Point = {point, 15, 20}.
> Point = {point, 15, 20}.
> {point, X, Y} = Point.
> Point = {point, 15, 20}.
> {point, X, Y} = Point.
> X.
15
> Point = {point, 15, 20}.
> {point, X, Y} = Point.
> X.
15
> Y.
20
Lists
Lists


Head and Tail
Lists


Head and Tail


    [H|T]
> L = [1, 2, 3, 4].
> L = [1, 2, 3, 4].
> [H|T] = L.
> L = [1, 2, 3, 4].
> [H|T] = L.
> H.
1
> L = [1, 2, 3, 4].
> [H|T] = L.
> H.
1
> T.
[2, 3, 4]
Functions
double(X) -> X * 2.
fact(0) -> 1;
fact(X) -> X * fact(X - 1).
sum([]) -> 0;
sum([H|T]) -> H + sum(T).
sum([]) -> 0;
sum([H|T]) -> H + sum(T).

map(_, [])    -> [];
map(F, [H|T]) -> [F(H) | map(F, T)].
sum([]) -> 0;
sum([H|T]) -> H + sum(T).

map(_, [])    -> [];
map(F, [H|T]) -> [F(H) | map(F, T)].

...
area({rectangle, Width, Height}) -> Width * Height;
area({square, X})                -> X * X;
area({circle, R})                -> 3.14159 * R.
area({rectangle, Width, Height}) -> Width * Height;
area({square, X})                -> X * X;
area({circle, R})                -> 3.14159 * R.

> area({square, 3}).
9
class Rectangle
  def initialize(width, height)
    @width = width
    @height = height
  end

  def area
    @width * height
  end
end

class Square
  def initialize(x)
    @x = x
  end

  def area
    @x * @x
  end
end

class Circle
  def initialize(r)
    @r = r
  end

  def area
    3.14159 * @r
  end
end
def area(*args)
  case args[0]
  when :rectangle
    args[1] * args[2]
  when :square
    args[1] * args[1]
  when :circle
    3.141519 * args[1]
  end
end
Concurrency
Pid = spawn(Fun).
Pid ! Message.
receive
  Pattern -> Expression;
  Pattern -> Expression
end
Remember map?




map(_, [])    -> [];
map(F, [H|T]) -> [F(H) | map(F, T)].
pmap(F, L) ->
  Parent = self(),
  Pids = map(fun(I) ->
                spawn(fun() ->
                  Parent ! {self(), F(I)}
                end)
              end, L),
  gather(Pids).
pmap(F, L) ->
  Parent = self(),
  Pids = map(fun(I) ->
                spawn(fun() ->
                  Parent ! {self(), F(I)}
                end)
              end, L),
  gather(Pids).

gather([]) -> [];
gather([Pid|T]) ->
  receive
    {Pid, Result} -> [Result|gather(T)]
  end.
xN
Distribution Oriented
More interesting things about Erlang
More interesting things about Erlang




   Error control, even distributed
More interesting things about Erlang




   Error control, even distributed
                 OTP
More interesting things about Erlang




  Error control, even distributed
                OTP
Web development with ChicagoBoss
You should try Erlang if...
...you want to play with functional programming
...you're interested in writing parallel programs
2
Haskell
Created by Simon Peyton Jones in 1990
Pure functional language
Pure functional language




    Lazy evaluation
Pure functional language




       Lazy evaluation




Strict but powerful type system
mymap _ [] = []
mymap f (x:xs) = f x : map f xs
Haskell Type System
Haskell Type System




      Strong
Haskell Type System




      Strong
       Static
Haskell Type System




      Strong
       Static
     Inferred
Char
Char
[Char]
Char
[Char]
String
Char
[Char]
String
Integer
Char
[Char]
String
Integer
([Char], Integer)
data BookInfo = Book Int String [String]
data BookInfo = Book Int String [String]

myBook = Book 987987 "About Wadus" ["Fulano", "Mengano"]
data Color =   Red
           |   Yellow
           |   Green
           |   RGB Int Int Int
data Color =   Red
           |   Yellow
           |   Green
           |   RGB Int Int Int

yellow = Yellow
black = RGB 0 0 0
Type System + Pattern Matching
data Shape = Circle Float
           | Rectangle Float Float
           | Square Float
data Shape = Circle Float
           | Rectangle Float Float
           | Square Float

area (Circle r)      = r * 3.14
area (Rectangle w h) = w * h
area (Square x)      = x * x
data Shape = Circle Float
           | Rectangle Float Float
           | Square Float

area (Circle r)      = r * 3.14
area (Rectangle w h) = w * h
area (Square x)      = x * x

circle = Circle 5
rectangle = Rectangle 2 3
square = Square 1.5
data Shape = Circle Float
           | Rectangle Float Float
           | Square Float

area (Circle r)      = r * 3.14
area (Rectangle w h) = w * h
area (Square x)      = x * x

circle = Circle 5
rectangle = Rectangle 2 3
square = Square 1.5

-- > area circle
-- 15.700001
data Shape = Circle Float
           | Rectangle Float Float
           | Square Float

area (Circle r)      = r * 3.14
area (Rectangle w h) = w * h
area (Square x)      = x * x

circle = Circle 5
rectangle = Rectangle 2 3
square = Square 1.5

-- > area circle
-- 15.700001

-- > :type area
-- area :: Shape -> Float
More Functional Programming
square_all list = map (n -> n^2) list




> square_all [1, 2, 5]
[1,4,25]
square_all list = map (n -> n^2) list
square_all list = map (^2) list



> square_all [1, 2, 5]
[1,4,25]
square_all list = map (n -> n^2) list
square_all list = map (^2) list
square_all = map (^2)

> square_all [1, 2, 5]
[1,4,25]
Laziness
@articles = Article.where(:status => 'published')
@articles = Article.where(:status => 'published')




<% @articles.each do |article| %>
<h2><%= article.title %></h2>
<p><%= article.body %></p>
<% end %>
@articles = Article.where(:status => 'published')




<% @articles.limit(5).each do |article| %>
<h2><%= article.title %></h2>
<p><%= article.body %></p>
<% end %>
wadus 1 + 2
iforelse cond a b = if cond
                    then a
                    else b
iforelse cond a b = if cond
                    then a
                    else b

> iforelse True (1 + 2) (3 + 4)
3
iforelse cond a b = if cond
                    then a
                    else b

> iforelse True (1 + 2) (3 + 4)
3
> iforelse False (1 + 2) (3 + 4)
7
You should try Haskell if...
...you tried the functional paradigm
       and want to go further
...you want to see your belief
“types are useless” challenged
...you want to write extremelly
efficient programs in a high level
             language
3
Common Lisp
Invented by John McCarthy in 1958
Invented by John McCarthy in 1958
Lots of
Irritating
Superfluou
s
Parenthese
s
HOMOICONIC
“property of some
programming languages,
    in which the primary
        representation of
 programs is also a data
  structure in a primitive
     type of the language
                    itself”
CODE = DATA
(1 2 3 ("wadus" 5) (1.3 1.7))
(1 2 3 ("wadus" 5) (1.3 1.7))

[1, 2, 3, ["wadus", 5], [1.3, 1.7]]
(+ (* 3 5) (/ 10 2))
(+ (* 3 5) (/ 10 2))

         20
(1 2 3 ("wadus" 5) (1.3 1.7))

    (+ (* 3 5) (/ 10 2))
(1 2 3 ("wadus" 5) (1.3 1.7))

    (+ (* 3 5) (/ 10 2))


         CODE = DATA
Lisp has no syntax
Valid Lisp code is composed of
  lists whose first element is the
name of a function and the rest are
       the parameters passed
Macros
Metaprogramming
Metaprogramming is creating and manipulating
                   code
Metaprogramming is creating and manipulating
                   code                   lists
LISt Processing
(defmacro backwards (code)
(defmacro backwards (code)
  (reverse code))
(defmacro backwards (code)
  (reverse code))

(backwards
  ("wadus" print))
(defmacro backwards (code)
  (reverse code))

(backwards
  ("wadus" print))

(print "wadus")
You should try Common Lisp if...
...you want to take a flight
 in a retrofuturist jetpack
...you like metaprogramming
...you want to try one of the
  most abstraction-capable
and still efficient languages
...you want to understand
why Lisp-lovers love Lisp so
           much
4
Created by Rick Hickey in 2007
Lisp meets XXI century
Lisp meets the JVM
Lisp meets the JVM


Lisp meets purelly functional programming
Lisp meets the JVM


Lisp meets purelly functional programming


           Lisp meets laziness
Lisp meets the JVM


Lisp meets purelly functional programming


           Lisp meets laziness


         Lisp meets concurrency
Clojure is a Lisp
More laziness
(def whole-numbers (iterate inc 1))
(def whole-numbers (iterate inc 1))

(first whole-numbers)
; 1
(def whole-numbers (iterate inc 1))

(first whole-numbers)
; 1

(take 5 whole-numbers)
; (1 2 3 4 5)
(def whole-numbers (iterate inc 1))

(first whole-numbers)
; 1

(take 5 whole-numbers)
; (1 2 3 4 5)

(last whole-numbers) ; => CATACROKER!
(defn fibonacci []
  (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1])))
(defn fibonacci []
  (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1])))

(take 20 (fibonacci))
; (0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
2584 4181)
STM
(def fulano-balance (ref 100))
(def mengano-balance (ref 80))
(def fulano-balance (ref 100))
(def mengano-balance (ref 80))

(dosync
  (ref-set fulano-balance (+ @fulano-balance 20))
  (ref-set mengano-balance (- @mengano-balance 20)))
(def fulano-balance (ref 100))
(def mengano-balance (ref 80))

(dosync
  (ref-set fulano-balance (+ @fulano-balance 20))
  (ref-set mengano-balance (- @mengano-balance 20)))

@fulano-balance
; 120
(def fulano-balance (ref 100))
(def mengano-balance (ref 80))

(dosync
  (ref-set fulano-balance (+ @fulano-balance 20))
  (ref-set mengano-balance (- @mengano-balance 20)))

@fulano-balance
; 120
@mengano-balance
; 60
(dosync
  (alter fulano-balance + 20)
  (alter mengano-balance - 20))
(dosync
  (alter fulano-balance + 20)
  (alter mengano-balance - 20))

@fulano-balance
; 140
(dosync
  (alter fulano-balance + 20)
  (alter mengano-balance - 20))

@fulano-balance
; 140
@mengano-balance
; 40
More interesting things about Clojure
More interesting things about Clojure




Web development with Compojure
More interesting things about Clojure




Web development with Compojure
You should try Clojure if...
...you like both Lisp and the functional approach
...you need/want Java/JVM interoperability
5
Ujfalusi
<
>
-
http://github.com/porras/
          ujfalusi
You should create your own language if...
...you want to learn about how
   languages internally work
...you want to have fun
doing something very geek
...your favorite player
leaves your team and
   breaks your heart
And the
winner
is...
ME!
Fun
New abstractions
New tools
Polyglotism
Go out
Play
Break something
Have fun
Thank you! :)

Five Languages in a Moment

Editor's Notes