LISP: How I Learned To Stop Worrying And Love Parantheses

751 views

Published on

My slides from Try {harder} 2012

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
751
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • LISP: How I Learned To Stop Worrying And Love Parantheses

    1. 1. Didn’t code in ages.HaXe got in the way. LISP
    2. 2. or how I learned to stop worrying and love parentheses
    3. 3. ; Clojure - a new dialect of LISP(defn hello [name] (println "Hello," name, "!"))(hello "TryHarder")
    4. 4. but we’re going to talk about more than just LISP
    5. 5. what is functional programming?
    6. 6. the history of functional programming
    7. 7. why did functional programming become so popular (again)?
    8. 8. or commonly used OO the differences between FP and imperative programming
    9. 9. analyze the current usage of object-oriented languages
    10. 10. accidental complexity
    11. 11. let’s have a look at object-orientedprogramming languages and their programming model
    12. 12. there are lots and lots of object-oriented languages out there
    13. 13. JAVA C# AS3HaXePython Ruby PHP ...
    14. 14. but actually they aren’t really that different
    15. 15. similar features:Still we have religiousfights about them! Classes Inheritance Polymorphism Closures imperative ...
    16. 16. Still we have religiousfights about them! slightly different styles: Indentation Semicolons Syntactic sugar ...
    17. 17. is this the holy grail of programming?are we done?
    18. 18. accidental complexity “Accidental complexity is complexity that arises incomputer programs or their development process which is non-essential to the problem to be solved. While essential complexity is inherent and unavoidable,accidental complexity is caused by the approach chosen to solve the problem.”
    19. 19. Causes side effectsFactories or Dependency variables vs. valuesInjection are a sign ofAccidental Complexity class Rhino { private var _position:Point; public function Rhino(position:Point) { _position = position; or _position = position.clone(); } }
    20. 20. Side effectsEveryone needs toobserve everyone. observers / dispatching eventsThe environment stopswhen observing it. var rhino:Rhino = new Rhino(); var water:Water = new Water(); rhino.addEventlistender(Event.RHINO_JUMP, water.splash);
    21. 21. mutability and stateMutability and state are accidental complexity.
    22. 22. We are to familiar withOO to see the problems. Most programmers see syntax and and expressivity as the key features in a programming language. Whereas we really run into problems understanding larger applications and also the effect that changes will take are very hard to predict.
    23. 23. A builder doesn’t need to building blocksknow what happensinside a brick.Pointers in C Object-oriented programming/design is meant to simplify our lives by using object-oriented modules/libraries as our building blocks for our program. But without knowing the internals this is almost impossible. (Pure) Functions are a far better fit for this purpose.
    24. 24. “Civilization advances by extending thenumber of important operations which we can perform without thinking.” Alfred North Whitehead
    25. 25. now imagine you don’t know anything aboutobject-oriented programming
    26. 26. the history of functional-programming
    27. 27. Math theory:Definition of functionsTheir applicationFunctional languageswere developed to have aclearer approach tomathematics lambda calculus by Alonzo Church in 1930
    28. 28. functional-programming languages 1959 Lisp 1975 ML, FP, Scheme 1986 Standard ML 1990 Haskell, Erlang 2000 OCaml 2003 Scala 2005 F# 2007 Clojure
    29. 29. what is functional-programming?
    30. 30. They talk aboutalgorithms, ds, recursion,scope ... on page 216they introduceASSIGNEMENTAvailable for free online.
    31. 31. In a restricted sense: functional programming meansprogramming without mutable variables, assignments, loops or imperative control structures.
    32. 32. In a wider sense: functional programming means to focus on functions.
    33. 33. In particular: functions can be values that are produced, can be consumed and composed.
    34. 34. what is essential in a languagefor functional programming?
    35. 35. higher-order functionIt has to be possible to use functions as in input or return values in other functions
    36. 36. function curryMe(input:Function):Function{ return curry(input, 2);}
    37. 37. first-class functionsIt has to be possible to declare functions anywhere, even inside other functions
    38. 38. function outerFunction():int{ function innerFunction():int { return 0; } return innerFunction();}
    39. 39. recursionIt has to be possible to call functions recursively
    40. 40. function sum(xs:Array):int{ if(xs.length == 0) return 0; return xs.shift() + sum(xs);}
    41. 41. pure functions Take and/or return values Local scope: no side effects Same arguments: same resultEasy to understand, change & test
    42. 42. function pure(x:int, y:int):int{ return x + y;}var _y:int = 1000;function notPure(x:int):int{ return x + _y;}
    43. 43. No classes, what types data-structuresdo we have??Just use basic types(Lists) and operate onthem.Basically the same as wealready.Code == Data "It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures." - Alan J. Perlis
    44. 44. why did functional-programming didbecome so popular (again) recently?
    45. 45. Clojure F# Scalanew functional-programming languages are popping up left and right
    46. 46. ClojureF#Scala Moores law
    47. 47. Single-Threaded clockspeed has stalled.Number of coresincreases.In a couple of years wewill have laptops with32 or 64 cores.
    48. 48. examples
    49. 49. //AS3a + b;a * b;;Clojure(+ a b)(* a b)
    50. 50. //AS3Math.max(a, b);;Clojure(max a b)
    51. 51. //AS3function add(x:int,y:int):int{ return x + y;};Clojure(defn add [x, y] (+ x y))
    52. 52. //AS3function fibonacci( a:int ):int{ if( a == 0 || a == 1 ) return a; else return fib( a - 1 ) + fib( a - 2 );}
    53. 53. ;Clojure(defn fib [n] (if (= n 0) 0 (if (= n 1) 1 (+ (fib (- n 1)) (fib (- n 2))))))
    54. 54. ;Clojure - whole fibonacci sequence(defn lazy-seq-fibo ([] (concat [0 1] (lazy-seq-fibo 0 1))) ([a b] (let [n (+ a b)] (lazy-seq (cons n (lazy-seq-fibo b n))))))
    55. 55. (defn neighbours [[x y]] (for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])] [(+ dx x) (+ dy y)]))(defn step [cells] (set (for [[loc n] (frequencies (mapcat neighbours cells)) :when (or (= n 3) (and (= n 2) (cells loc)))] loc)))
    56. 56. Runs on the JVMInteroperability withJava libraries why clojure?

    ×