Posts for the month of November 2014

Julia is awesome!

One coukd tell it comes from MIT,

julia> function sum(f, x, y)
           if x > y
               0
           else
               f(x) + sum(f, (x+1), y)
           end
       end
sum (generic function with 1 method)

julia> sum(x->x, 1, 10)
55

julia> sum(x->x*x, 1, 10)
385

julia> map(x->x*x, [1,2,3,4,5])
5-element Array{Int64,1}:
  1
  4
  9
 16
 25

julia> filter(x -> mod(x,2) == 0, [1,2,3,4,5])
2-element Array{Int64,1}:
 2
 4

julia> reduce(+, map(square, [1,2,4,5]))
46

julia> compose(f, g) = x -> f(g(x))
compose (generic function with 1 method)

julia> twice(f) = compose(f,f)
twice (generic function with 1 method)

julia> twice(square)(2)
16

julia> make_adder(n) = x -> x+n
make_adder (generic function with 1 method)

julia> add1 = make_adder(1)
(anonymous function)

julia> add1(5)
6

Humiliation

Just watched C9 Lectures: Dr. Erik Meijer - Functional Programming where he, perhaps unintentionally, is comparing Haskell to C#.

What a humiliation (of C#, not dr. Maijer, its meaningless verbosity, syntactic noise), especially when he is comparing such a gem of declarative programming

not False = True
not True  = False

to some class-based dynamic-dispatch in C# in episode 4 around 0:13.

Watch it.

btw, to fully appreciate the beauty we should look at the type-declaration first:

data Bool = True | False

and understand how not is defined using pattern-matching on data-constructors (True and False) of an algebraic data-type (Bool).

This means that when Haskell evaluates the expression True it produces a value of type Bool with corresponds to the truth. This is why such expression is called data-constructor - it produces a value of an either-of algebraic data-type. Likewise for False expression.