DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Notes about Why Functional Programming Matters


John Hughes: Why Functional Programming Matters

  • The key points of a programming language is its modularization capability: it can divide a big problem into some smaller sub-problems; then glue them together. "Modularity is the key to successful programming. Languages that aim to improve productivity must support modular programming well."

  • FP language has 2 ways to glue sub-problems together:

  • High-order functions: the functions using other functions as its parameter(s);

  • Lazy evaluation: the generator-selector model;

Analysis of "4.1 Newton-Raphson Square Roots"

Convert the functions of Newton-Raphson algorithm in this paper into Clojure functions:

next n x = (x + n/x)/2 => #(/ (+ % (/ n %)) 2)

repeat (next n) a0 => (iterate #(/ (+ % (/ n %)) 2) a0)

So the 8th iteration in decimal is:

(double (nth (iterate #(/ (+ % (/ n %)) 2) a0) 8))

Here the point is the function "iterate" returns a lazy sequence, which is to say, if you evaluate "(iterate inc 5)" in clojure repl, you got a infinite list (you have to press Ctrl-c to interrupt the output). You use "nth" to get the nth element in this list without worrying about your memory space.



Published

Nov 26, 2013

Last Updated

Nov 26, 2013

Category

Tech

Tags

  • functional programming 9

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor