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.

Introduction to Systematic Programming – Part 3 Originally authored by Dara Monasch Week 3 of Introduction to Systematic Program Design was definitely a LOT more video content than I was used to from before, and I can safely say that having started watching the videos from Week 4 already, it’s only going to get crazier from here on out! That said, as always, here are the links to the starting posts from this series, in case you missed out on them: Intro to “Introduction to Systematic Programming” Course Introduction to Systematic Programming – Week 1 Introduction to Systematic Programming - Week 2 So Week 3 of class focused primarily on learning How to Design Data and I’ll walk you through each lesson we learned step by step as best I can. Lesson 1: cond Expressions It’s made clear to the class in this lesson that the designing of data is a point of leverage in designing programs. Conditional expressions, or cond expressions, allow us to program conditional behavior when there are two or more cases. Standard cond expression template:

CoffeeScript openvmtil - openVm : Tookit for Implementing (and exploring) Languages - a bottom-up vm that is an extensible scripting language Lastest downloads at An Exploration of Language Theory - and its Machine Implementation Imagine a low level, optimizing, virtual machine (like llvm) that is an extensible scripting language and that is small enough to be easily verified, where even the runtime is reconfigurable - minimize that. Current focus (to do) : minimal bootstrap, self-hosting, patterns/sets, logic, tail call, type checking, gui The Turing Machine, the Lambda Calculus and Type/Category Theory are the theoretical foundations. With an ideal that the best (cross platform) virtual machine or common language runtime is a minimal but maximally extensible, optimally and simply compiled (rpn) language, certainly "human readable", but also maintainable, learnable and extensible. Current Focus and Direction : Goals : Explore the power of simplicity as a software design principle - how simple can it be and still be totally effective. Acknowledgements : Special thanks for : Haskell B.

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

Dynamic Programming Wow, it’s been a while since I’ve written anything here. Between changing jobs, working on my PhD and moving to a new country I guess you could say I’ve been pretty busy. But at the same time, together with all these changes in my life there’s a ton of new things that I’m learning almost every day. So with that covered lets move to the important stuff. On this post I wanted to talk about Dynamic Programming. So what exactly is Dynamic Programming? The classic example to explain dynamic programming is the fibonacci computation, so I’ll also go with that. F(n) = F(n-1) + F(n-2) and F(1) = F(2) = 1 This means that the sequence of the first 10 fibonacci numbers would go: You might also find it defined as: And so the sequence would be: For the purposes of this post this difference is irrelevant but I’ll stick to the first one. Now, this recursive definition translates naturally and quite neatly into the following recursive method: You can even make that a one liner: So, what’s the alternative?

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

Program Languages Research 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

Introduction to Systematic Programming – Week 1 - Marketing & PR - Infragis For those of you just tuning in, check out the “Intro” to the Introduction to Systematic Programming series here for some more background on what we’re about to get into. If you’ve been here since last week, I’m sure you’re waiting with bated breath to hear how the first week of classes went. Well, the wait is over! Week 1 consisted of 9 lessons centered around the topic of Primitives in Dr. Racket. I’ll go through each lesson here one by one, giving a quick description of the main topic points we covered and any interesting additional tidbits I may have picked up. Lesson 1: Introduction to Systematic Program Design The primary point covered in this first lesson is that, “Computation is what makes ‘things’ happen!” Next, we reviewed the question, “What’s the ‘Design Method’?” Lesson 2: Expressions Dr. In Dr. For example, to have Dr. Additional notes from this section: ; designates a line to be commented out #i designates an inexact number Lesson 3: Evaluation The Rules in Dr. Strings Images Text

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]

Fortran Literature

Related: