43 points · by pieterr · 8y ago · 54 comments
public static List generate(int series) {
return Stream.iterate(new int[]{0, 1}, s -> new int[]{s[1], s[0] + s[1]})
.limit(series)
.map(n -> n[0])
.collect(toList());
}
as opposed to this: public int fib(int n) {
int x = 0, y = 1, z = 1;
for (int i = 0; i < n; i++) {
x = y;
y = z;
z = x + y;
}
return x;
} def fib(n: Int, a: Int, b: Int): Int = {
if (n > 0)
fib(n - 1, b, a + b)
else
a
}
Unfortunately Java cannot express self tail recursive functions and Scala too can't express mutual tail recursive functions, as that would require runtime support.But you know what? That mutable loop is totally fine, because any mutation it has is encapsulated, the function itself being pure.
Python iterative solution
1. def fib(n):
2. i, curr, next = 0, 0, 1
3. while i < n:
4. i += 1
5. curr, next = next, curr + next
6. return curr
Python tail-recursive solution* 1. def fib(n):
2. def fib_tail(i, curr, next): #1
3. if i >= n: #2
4. return curr #3
5. return fib_tail(i+1, next, curr+next) #4
6. return fib_tail(0, 0, 1)
Some explanation of the 1-1 conversion process:#1. We need a helper function, `fib_tail` to keep track of the variables we update in the while loop. (lines 4-5 in iterative)
#2-3. Return `curr` when the opposite of the while condition is met. (lines 3,6 in iterative)
#4. Recursively call `fib_tail`, using same update logic as while loop body when passing arguments. (line 5 in iterative)
#5. Call helper function with initial values for the additional variables (line 1 in iterative)
* We're going to pretend that Python supports tail recursion. It doesn't, but I figure more people are familiar with python; think of it more as pseudocode for instruction. You could easily implement the above in something like Scheme
fib = 0:1:zipWith (+) fib (drop 1 fib)
[I know that `drop 1` is just `tail`, but this one makes the intent clearer for those who may not know Haskell]. let generated: Vec<i32> =
itertools::iterate((0, 1), |&(a, b)| (b, a + b))
.map(|s| s.0)
.take(10)
.collect();
Arguably, this is closer to the matrix form of Fibonacci series[1]. In addition, the same code structure can generalize to any series with a recursive definition in the form of: a_n = f(a_{n-1}, a_{n-2}, ..., a_{n-k}) - which by some standard expresses intent better. Especially, for some k, the mental overhead of a loop version is too much to bare :)[1] https://wikimedia.org/api/rest_v1/media/math/render/svg/c795...
Toy examples are toys; they're usually easily broken, since the simplicity required for pedagogy leaves a poor ratio of problem difficulty to solution power. I wouldn't write the example code as a stream either, but I much prefer the approach when the complexity level increases only slightly.
Unfortunately the term has become so muddled in the common usage that if at this point, if someone wants to call APL functional, they probably can.
Functional Programming is programming with mathematical functions (aka pure functions).
And an FP language is one that has features, but also a culture that encourage FP as the main paradigm for solving problems.
Examples of FP languages: Haskell, SML, OCaml, Scala and Clojure.
Common Lisp, Emacs Lisp interestingly are not. Python, Java and C# are not. JavaScript is not, although it has a cultural shift in progress with pretty good results. Go at this point is actually anti-FP, if such a thing was possible.
Also I don't see how Go can be considered actively "anti-FP" when it supports passing closures as arguments, which many, many languages do not. I don't understand why (some) FP and Go partisans feel such a need to see one another as enemies.
Most languages are multi paradigm, even clojure is used with an object-like entity component system, while there are papers that push Haskell as the best language for imperative programming. The word is kind of meaningless when applied to languages but more meaningful when applied to code.
As a side note, there are a LOT of considerations that are implicit within a language when you do functional programming. Let me explain. Let's consider a `map` function. There are at least 2 ways to write a non-generic map function:
func MapSafe(f func(int) int, l []int) []int {
retVal := make([]int, len(l)
for i := range l {
retVal[i] = f(l[i])
}
return retVal
}
func MapClobber(f func(int) int, l []int) []int {
for i := range l {
l[i] = f(l[i])
}
}
Any good FP-er will tell you clobbering data is NOT a good idea - that functional programming is all about immutable data. Yeah, the immutable data version however, allocates memory. So you either need:a) impressive compiler building skills to reason that for some application of `map` clobbering can be done, and for others it cannot; b) failing to do that, you would need a very awesome GC that handles memory that work on a very short timescale (that is, within callframes)
Rust is nice in the sense that the compiler does a LOT of the heavy lifting, but it's far from userfriendly. Requiring users to manage and be clear about memory ownership is a Hard Sell (though I will concede, a Good Idea). Haskell is another language which relies heavily on the compiler, and GHC is an amazing compiler. However, performance for Haskell isn't that great. It's on the opposite end of the scale: super user friendly, offloads a lot of work to the compiler, but suffers in performance.
The problem is I don't think we're there yet. Not with Go, but with functional languages. State wrangling is still a problem. Running away from it by hiding under layers of functional language works for a small number of problems (business rules,etc) but in my view, not the majority of the problems. Programmers don't spend a lot of time on high level problems.
A long time ago, there was a compiler for Haskell called JHC. It was nice because you could use it to write C, which would expose the underlying state for the programmer to modify to her heart's content. The project's been dead for 5-6 years now I think. GHC has a weird --from-c option that I've never been able to use mainly because you needed to recompile GHC from scratch, which again is something I struggle with and rapidly give up.
Exactly. To which the following must be added: Functions are mappings from values to values. It makes no sense to try to do functional programming in a language whose semantics does not provide a rich enough supply of values.
> And an FP language is one that has features, but also a culture that encourage FP as the main paradigm for solving problems.
Culture matters very little. What really matters is what the language's semantics allows.
Neither it is true that FP guarantees more readability or understandability, which actually depends a lot on what your are used with. If you are used to imperative, FP will probably be a real pain. It is not even true that shorter functions are in general more readable the longer ones: I can immediately understand pages of code and struggle on two lines, depending on what they do and how they are written.
The actual point of FP, as I see it, is that it decouples a function from the context it is executed into. A pure function is not influenced by its context (the global status) and does not influence it (it has no side effect). Actually, for a pure function the context does not exist. So if you want to read or understand it, you can ignore the context; and viceversa you can ignore the function when studying any other. The only interaction between different functions remain the explicit calls, which are easier to track than the implicit coupling given by the global context.
So that is the advice from FP that I would give to every programmer: when writing a function, try to use the global context as few as possible, possibly even not at all.
All the other stuff with lists, zipping and lambdas is fun and useful, but it is not the real heart of FP.
I 100% agree with this. To me, a rough measure of code simplicity is the amount of additional context you need to be able to reason about any specific function or object. Pure, statically-typed functions are close to ideal - all the context you need is encapsulated within the function definition itself.
Functions in dynamically-typed languages are trickier - in addition to the function definition, you also need to know how the function was called, since there's no guarantees about the arguments that were passed in. Impure functions are the worse - if the function interacts with global state, then you'll need to be aware of every callsite that interacts with global state. Practically speaking, that means you can't reason about anything in isolation - you always need to hold the entire program in your head. Hence the whole "globals are evil" mantra.
Thinking in terms of functional purity gives a convenient way to write clean code. I definitely recommend every programmer at least be familiar with the concept.
That's the thing I keep coming back to. FP simply allows you to do more with less code that has to be tested.
I would not consider lambda programming FP, although I sort of understand what is being said. Lambda is just the tiniest beginnings of FP.
When I started FP, I chose F#. I figured that way I could still write classes when I needed them. I also figured it would allow me to do things the "wrong" way and then learn by cleaning up.
I found over several projects that I eventually stopped using classes altogether. There just wasn't any need for them. So much of what TDD brings to the table can be boiled down to "ways to make sure you're not shooting yourself in the foot in OOP"
Sidebar: At one point, Dave (the author here) had the only TDD/Microtest framework for COBOL. It was a very cool idea. Dave's a really smart guy.
The other grouse I have with posts like these are they try to over-simplify FP - "using lambdas is FP" or something like that. I think this leads to folks thinking 'hmm - I know how lambdas work... so if I use lambdas, then it's good' which is missing the point completely.
I think one does need to 'grok' FP concepts completely - composition, higher order functions, tail recursion, immutabililty etc and gain an appreciation of FP vs procedural the hard way before they can hope to utilize it effectively.. I think those things help you think 'functionally' which is much more valuable in the long run rather than 'how do I bolt on FP on OOP'