Live data from Hacker News

Ask HN: When is pure functional programming beneficial?

news.ycombinator.com

61–70 of 86 posts

Re: Ask HN: When is pure functional programming beneficial?

#62
post #59

Earlier quoted context omitted.

It burns me up that Fibonacci numbers are used so frequently as an example of functional programming because it is a clear case of malpractice, particularly because it performs terribly without memoization. Even in the 1980s CS profs were trying to tell us how BASIC sucks but efficient Fibonacci is so easy to code up in BASIC. (I'd really be impressed with a system that could figure out the closed form based on the d…

I don't see why you can't write the closed form in a functional language as well. This only works for 1 = = 71 the approximation of Sqrt5 isn't good enough; you need to do more symbolic math so you can eliminate that term. -module('fib'). -export([go/1]). go(N) when N throw(badarg); go(N) -> Sqrt5 = math:sqrt(5), round(1.0 / Sqrt5 * math:pow((1 + Sqrt5) / 2.0, N) - 1.0 / Sqrt5 * math:pow((1 - Sqrt5) / 2.0, N)). rp(li…

The question is if you can have a system that can derive the closed form from the definition. Not one you can feed it to. Right?

Similarly, it may be faster to use the repeated squares approach to get a large value fib, and I don't know any system that can get that from the recursive definition, either.

Re: Ask HN: When is pure functional programming beneficial?

#63
post #62
post #59

Earlier quoted context omitted.

I don't see why you can't write the closed form in a functional language as well. This only works for 1 = = 71 the approximation of Sqrt5 isn't good enough; you need to do more symbolic math so you can eliminate that term. -module('fib'). -export([go/1]). go(N) when N throw(badarg); go(N) -> Sqrt5 = math:sqrt(5), round(1.0 / Sqrt5 * math:pow((1 + Sqrt5) / 2.0, N) - 1.0 / Sqrt5 * math:pow((1 - Sqrt5) / 2.0, N)). rp(li…

The question is if you can have a system that can derive the closed form from the definition. Not one you can feed it to. Right? Similarly, it may be faster to use the repeated squares approach to get a large value fib, and I don't know any system that can get that from the recursive definition, either.

Nonfunctional programming doesn't give you that either, so I don't see the problem?

If you use the iterative / recursive form, runtime performance isn't great. If you use a lookup table, performance is great, but space might suffer (but then, how many values do you really need?). If you want to use something better, you've got to write it yourself. Maybe one day an optimizing compiler will figure it out, but it's more likely to figure out you never call it with N > 10, and precompute, and that's fine.

Re: Ask HN: When is pure functional programming beneficial?

#64
post #63
post #62

Earlier quoted context omitted.

The question is if you can have a system that can derive the closed form from the definition. Not one you can feed it to. Right? Similarly, it may be faster to use the repeated squares approach to get a large value fib, and I don't know any system that can get that from the recursive definition, either.

Nonfunctional programming doesn't give you that either, so I don't see the problem? If you use the iterative / recursive form, runtime performance isn't great. If you use a lookup table, performance is great, but space might suffer (but then, how many values do you really need?). If you want to use something better, you've got to write it yourself. Maybe one day an optimizing compiler will figure it out, but it's mor…

Right, but the post is "I'd really be impressed with a system that could figure out the closed form based on the definition." Point seeming that coding up a recursive process based on the definition doesn't seem to be the best reason to teach something that way.

This goes back to a ton of the talk on early compilers and systems. They would push that you should lean heavily on the system to move from definitions to optimal solutions. (This later got changed heavily to leaning only on the compiler, but the same idea applies.)

Re: Ask HN: When is pure functional programming beneficial?

#65
post #64
post #63

Earlier quoted context omitted.

Nonfunctional programming doesn't give you that either, so I don't see the problem? If you use the iterative / recursive form, runtime performance isn't great. If you use a lookup table, performance is great, but space might suffer (but then, how many values do you really need?). If you want to use something better, you've got to write it yourself. Maybe one day an optimizing compiler will figure it out, but it's mor…

Right, but the post is "I'd really be impressed with a system that could figure out the closed form based on the definition." Point seeming that coding up a recursive process based on the definition doesn't seem to be the best reason to teach something that way. This goes back to a ton of the talk on early compilers and systems. They would push that you should lean heavily on the system to move from definitions to op…

That was parenthetical, I was responding to "It burns me up that Fibonacci numbers are used so frequently as an example of functional programming because it is a clear case of malpractice"

Re: Ask HN: When is pure functional programming beneficial?

#66
post #65
post #64

Earlier quoted context omitted.

Right, but the post is "I'd really be impressed with a system that could figure out the closed form based on the definition." Point seeming that coding up a recursive process based on the definition doesn't seem to be the best reason to teach something that way. This goes back to a ton of the talk on early compilers and systems. They would push that you should lean heavily on the system to move from definitions to op…

That was parenthetical, I was responding to "It burns me up that Fibonacci numbers are used so frequently as an example of functional programming because it is a clear case of malpractice"

Right, I took that to be that the complaint is the recursive definition is used to teach it. Stated differently, ignoring the parenthetical takes the weakest interpretation of that post.

Re: Ask HN: When is pure functional programming beneficial?

#67
post #58

Earlier quoted context omitted.

> Places where the ecosystem is not quite as mature. If you're building a server and have to interact with Cloud services in Haskell, you'll have a bad time. Why does a server that interacts with cloud services not fit well with functional programming?

Read what you’ve quoted. They say the ecosystem is lacking, not that the issue is inherent in FP.

They wrote about "places where the ecosystem is not quite as mature", and then referenced interacting with cloud services. I read that as alluding to the rapid development and instability of some cloud services.

I've written plenty of non-FP code that interacts with less mature systems, and the problems I've always run into have been related to changing APIs and behaviors of the remote systems, not anything inherent to the language or paradigm I'm using.

I was wondering what it is about FP that makes it less suitable for unstable environments.

Re: Ask HN: When is pure functional programming beneficial?

#68
"FP works great for distributed systems, and most software written today is distributed."

According to Grokking Simplicity[1], good functional programming isn't about avoiding impure functions; instead extra care is given to them. These functions depend on the time they are called, so they are the most difficult to get right.

Compare to pure functions: given the same input, the results are always the same regardless of when the function was called. So they are easier to reason with.

There is actually a level that is even easier to reason about than pure functions: plain data.

So functional programmers prefer data over pure functions over impure functions.

The reason to avoid "leaks" is because impure functions cause everything that calls them to also become impure. However, it is OK to use local mutation contained to the function itself. Sometimes mutation is simpler and doesn't affect the purity of the function that contains it.

Another good book on practical functional programming is Domain Modeling Made Functional[2]. Actually all of Scott Wlaschin's material[3] is very good!

[1]: https://www.manning.com/books/grokking-simplicity

[2]: https://pragprog.com/titles/swdddf/domain-modeling-made-func...

[3]: https://fsharpforfunandprofit.com/

Re: Ask HN: When is pure functional programming beneficial?

#69

I wouldn't say there is any threshold where purely functional programming shines less. Fewer regressions and the system being more likely to "just work" makes it more fun to develop. So for interactive programs, servers, CLI tools, parsers et.c. purely functional programming is amazing. An elm developer reported that the prototype they wrote in elm ended up with less bugs than the actual production system. I personal…

> In F# ... 350k lines of C# were rewritten in 30K of F# code.

Not discounting the rest of your statements (I love FP for all the reasons), but I really despise - and discount - statements like the one above. I feel they are disingenuous:

"This large-ish program that grew over time with evolving needs and uses was rewritten at a later date once the requirements and use-cases were fully understood and - amazingly - we were able to greatly simplify the code!"

It probably could have been rewritten in BASIC and still resulted in fewer LOC. That's not to discount the null checks, "zero bugs", or other factors. My favorite part of programming in Haskell is that 99% of the time if it compiles it just works, and I can refactor code + add features without concern.

Re: Ask HN: When is pure functional programming beneficial?

#70
post #53

Earlier quoted context omitted.

Sadly, that page isn't loading for me. :( Mistake on my end? I'm going to guess that the shader version is a bit larger than the naive turtle version. Not that it isn't doable, but is tough evidence to hold for the benefits of the expressive power of functional code. :D

It is 74 lines of basically C code on a page that runs the shader with WebGL. On my iPad it renders at 60 fps with a parameter that changes. It’s really neat. It doesn’t look like “functional code” but it really is since the shader returns a pixel color for x and y. I bet there is some squarish kind of fractal that could be code golfed with but manipulation.

Odd that the page doesn't show me the code. For some reason, webgl is busted on my firefox at the moment. Not sure if that is my fault or something else. I'd expect it to still show the code, though. :(

At any rate, thanks for the link! Will try another computer later to see if it can load.

Post reply on HN