background preloader

Functional Programming For The Rest of Us

Functional Programming For The Rest of Us

Memoizing recursive functions via the fixed-point Y combinator ... If you like the article below, you might also enjoy: Recursion as fixed points Students of algebra are already familiar with recursion and fixed points. They just don't realize it. Consider an equation like "x = x2 - 2." If asked to solve for the value of x, a student might re-arrange the equation to use the quadratic formula. A fixed point of a function f is an input that is equal to its output; that is x is a fixed point of the function f if x = f(x). Define the function f such that f(v) = v2 - 2. f(-1) = (-1)2 - 2 = 1 - 2 = -1, and: f(2) = (2)2 - 2 = 4 - 2 = 2 or by graphing y = x and y = f(x): These are exactly the solutions to x = x2 - 2 given by Wolfram Alpha. The insight that powers the upcoming technique is the observation that any time we have a recursive definition of the form "x = f(x)," the meaning of x is going to be defined in terms of fixed points. The Y combinator is that trick. The Y combinator in theory Find a functional whose fixed point is the recursive function we seek.

CoffeeScript Functional Data Structures Out Of Nowhere I’ve been watching the Structure and Interpretation of Computer Programs videos recently while riding the Caltrain and enjoyed it quite a bit. For some reason I can’t quite grasp, I find these fun. Maybe it’s the fact that these are 20 years old now and still terribly relevant (especially for functional programming), maybe it’s the look of the attendance, very eighties, or maybe the obvious delectation Hal Abelson and Gerald Jay Sussman have teaching. Anyways, pretty intesting stuff. One of the thing they emphasize a lot during their lessons is the blurring line between data structures and functions when programming in a functional style extensively. Javascript Ruby Arc [source:ruby] arc> (def make_pair (a b) (fn (pick) (if (> pick 0) b a))) arc> (def head (p) (p 0)) arc> (def tail (p) (p 1)) arc> (tail (make_pair ‘a ‘b)) b [/source] It’s a very good illustration of how to introduce data structures in any functional language. Photo (and painting) by Rob Lee

Use functional programming techniques to write elegant JavaScript Introduction Functional programming languages have been in academia for quite some time, but historically they do not have extensive tools and libraries available. With the advent of Haskell in the .NET platform, functional programming is becoming more popular. Some traditional programming languages, such as C++ and JavaScript, import constructs and features from functional programming. Because functional programming encompasses a very different way of composing programs, programmers who are used to the imperative paradigm can find it difficult to learn. Functional programming concepts, including anonymous functions, different ways to call functions, and how to pass functions as arguments to other functions. Back to top Functional programming concepts Many developers know how to code in languages where you specify the method of solving a problem by describing "how." Listing 1. int factorial (int n) { if (n <= 0) return 1; else return n * factorial (n-1); } Listing 2. Anonymous functions Or

Backus&#039;s Idea of Functional Programming In my earlier post about John Backus, I promised to write something about his later work on functional programming languages. While I was in a doctors office getting treated for an awful cough, I re-read his 1977 Turing Award Lecture. Even 30 years later, it remains a fascinating read, and far from being dated, it’s positively astonishingly to see both how far-sighted Backus was, and how little progress we’ve actually made. Backus started from a pretty solid perspective. Almost all of the common programming languages that we see – ranging from Backus’s own 1950s version of Fortran, to the most modern languages we see widely used today (Java/C/C++), to the various languages that have been proposed as alternatives to those (Modula-3, Oberon, Ada), even to most of the supposedly functional programming languages (Lisp, ML) – all of them are ultimately based quite strongly on the von Neumann model of computing. That division stinks for a lot of reasons. Def InnerProduct ≡ (/+)º(α×)ºTranspose

Functional Javascript var Functional; Functional is the namespace for higher-order functions. Functional.install = function(except) This function copies all the public functions in Functional except itself into the global namespace. If the optional argument is present, functions named by its property names are not copied. Higher-order functions Functional.compose = function(fn...) Type: (a2 → a1) (a3 -> a2)… (a… -> an) -> a… -> a1 Returns a function that applies the last argument of this function to its input, and the penultimate argument to the result of the application, and so on. (1, 2, 3…, )() =def 1(2(3(…((…))))) compose('1+', '2*')(2)→ 5 Functional.sequence = function(fn...) Type: (a… → a1) (a1 -> a2) (a2 -> a3)… (an-1 -> an) -> a… -> an Same as compose, except applies the functions in argument-list order. (1, 2, 3…, )(…) =def (…(3(2(1(…))))) sequence('1+', '2*')(2)→ 6 Functional.map = function(fn, sequence, object) Type: (a ix → boolean) [a] -> [a] Applies fn to each element of sequence. map('1+', [1,2,3])→ [2, 3, 4]

Why functional programming? The canonical answer to that question is probably “Why functional programming matters“, but here’s a specific example that makes the case nicely. Neil Mitchell is working on Supero, an optimizing compiler for Haskell which includes some ideas from supercompilation. But that’s not important right now. What is important is the technique Mitchell uses in the blog post at the second link above. Functional programming is based on defining functions. def square(i: Int) = i * i In pure functional programming, where side-effects are not allowed (or at least kept securely behind bars), you can treat that definition as an equation; wherever you see square(foo) you can rewrite it as (foo * foo) and vice versa. The point is that you can’t do that with confidence for impure (i.e., having side-effects) languages. n = square(++i); …which clearly is not the same as… n = ++i * ++i; …at least in most cases. If you even had to think about it for one millisecond (or, like me, thought “That’s ugly! Cheat sheet

PHPUnit Support After you set up PHPUnit, Web IDE greatly helps you to run your tests. Namely, to quickly create test run configuration: Right-click the desired target: a directory or a PHP file in the Project view, or a test class/method name in the code editorChoose Run<name> to start or Create<name> to specify additional parameters That’s it! XML configuration file Advanced settings can be specified through PHPUnit configuration file. Test groups A test can be tagged as belonging to one or more groups using the @group annotation as shown below. The test is run if none of the specified groups is excluded and at least one group is included. Run/Debug Tests Before you execute tests, please set up PHP home directory (one that contains PHP executable) in Settings | PHP. Test results window You can easily navigate from tests results tree and stack trace to the corresponding source code location. When debugging your tests you get all the features, such as watches, expressions evaluation, etc. Test with pleasure!

Haskell and Scheme: Which One and Why? While I was waiting for stuff to install on my new machine, I was doing some browsing around the web, and came across an interesting article at a blog called “The Only Winning Move”, titled [Scheme Death Knell?]( It’s not a bad article at all, but I disagree with its conclusions, and I thought it would be interesting to discuss why. The article was brought on by *another* blog post, this one at [Lambda the Ultimate]( suggesting that someone translate Douglas Hofstadter’s columns introducing Scheme to replace the Scheme with Haskell. Josh over at TOWM was slightly irritated by this idea, and concludes that the only reason why people are suggesting that is because scheme is getting too old to be cool and trendy, and so they want to switch to something newer. I disagree. Syntax ——– Syntax is in many ways a minor issue. I think that syntactically, Haskell is a better language for beginners.

Functional reactive programming in C# for WPF I've been working on a library to wrap WPF dependency properties with signals so that data binding is easier to do in WPF when using C#: Dependency properties by themselves are like signals (time-varying values) but don't support direct transformation (i.e., map) or composition like signals do. rectangle().top().bind = canvas.height() - rectangle.height() meaning that the rectangle is stuck to the bottom of the canvas, even as the size of the canvas or rectangle changes. This is currently a source code release only. Functional Reactive Programming in F# (1) Hello, This message is the first of a series describing my exploration of Functional Reactive Programming (FRP). Although I already had quite a bit of experience with imperative reactive programming, I discovered FRP while reading the book of Paul Hudak: "The Haskell School of Expression". Articles about FRP have also been published by The Yale Haskell Group . I started my own implementation of a FRP library with 2 goals in mind: To gain a better understanding about this very peculiar domain. #light type Time = float type 'a Behavior = Time -> 'a Although this choice is correct, we rather use the next one. type 'a Behavior = Beh of (Time -> 'a) With this simple type, it is already easy to define some behavior. To ease the creation of behavior, we can write some useful combinators: constB: transforms a constant into a Behavior: //val constB : 'a -> 'a Behavior let constB v = let bf t = v in Beh bf let oneB = constB 1 let helloB = constB "Hello World!" We can then write:

Related: