Live data from Hacker News

What's functional programming all about? (2017)

lihaoyi.com

71–80 of 158 posts

Re: What's functional programming all about? (2017)

#71
post #70
post #68

Earlier quoted context omitted.

There is an interesting trend where things with a strong mathematical definition tend to have the advantage. "Functional Programming" doesn't have a precise definition at all as far as I know, so it is likely it doesn't refer to a real thing. Most of the paradigms are similar as they don't seem to actually mean anything. People seem to want to describe something (in today's article, data flow) but they don't quite ha…

It's funny you say that because I'd say FP has a much more well defined mathematical foundation compared to OOP. In fact, before it became as dominant as it is today, it was often criticized as being too scholarly and theoretical. It's built mostly on the foundations of Lambda Calculus (and maybe some Type Theory). I don't think OOP ever had nearly as strong of a mathematical/scholarly foundation as FP has

> It's built mostly on the foundations of Lambda Calculus (and maybe some Type Theory).

Can you describe a programming language that isn't based on lambda calculus? It is a universal model of computation.

Re: What's functional programming all about? (2017)

#72
post #66
post #65

The article is not about FP. > Languages like Java encourage patterns where you instantiate a half-baked object and then set the fields later. Maybe it did before 2010. For many years everyone prefers immutable objects (so that object and its builder have different types, or in simpler case -- no setters, only constructor initialization). You can see it in pretty much all frameworks. I'm ok with both functional and p…

Define what you mean by "everyone" -- there are times where the cost of immutability can be overwhelming, such as in high traffic systems with overly complex data structures which you are required to use because someone who should have known better insisted upon writing. (sorry, bitter personal experience) And yes, that is explicitly "modern" Java code written by a lead engineer and "java champion" in 2023.

Yes, I feel you. As I said, we often need to sacrifice immutability to performance, and that's ok. If they insisted using immutable structures in high-performance applications, then functional programming won't help anyway.

Re: What's functional programming all about? (2017)

#73

The article itself was well written, although I'd appreciate if the author was more "to the point" with the examples. FP never resonated with me and never fit the mental model I have for programming. I tried learning Prolog and Haskell a few times, and I never felt like I could reason about the code. This line from the article: "[..] With functional programming, whether in a typed language or not, it tends to be much…

I’ve wondered for some time how this breaks down along preferences for math vs (human) language, or for proofs/formula thinking vs algorithmic. I find FP concepts easy enough to grasp (provided they’re not demonstrated in e.g. Haskell) and even adjacent stuff like typeclasses or monads or what have you aren't a stumbling block, and I'm plenty comfortable with stuff like recursion. … but I'm firmly on the language-is-…

When I was younger, I found writing essays, mathematical proofs, and imperative code very similar activities. Functional programming was difficult, because I could not find the right way to think about it.

Over time, I have slowly become better at functional programming. But I'm now worse with proofs and essays due to the lack of practice.

Re: What's functional programming all about? (2017)

#74
post #68

As a programmer, I don't know if it's still relevant to make a strict separation between programming paradigms. You can use immutable types, pure functions, closures and so on in most languages. Conversely, you can define mutable types and imperative code in most functional programming languages. I'm always surprised reading comments on these topics, people saying they don't grasp FP. But don't we use higher-order fu…

There is an interesting trend where things with a strong mathematical definition tend to have the advantage. "Functional Programming" doesn't have a precise definition at all as far as I know, so it is likely it doesn't refer to a real thing. Most of the paradigms are similar as they don't seem to actually mean anything. People seem to want to describe something (in today's article, data flow) but they don't quite ha…

It's arguable that SQL is "basically applying and composing functions," given that its mathematical underpinnings lie in the relational algebra (hence relational databases). Further, while it's maybe technically correct to make statements such as, all programming languages are based on lambda calculus, a universal model of computation [1], this is about as precise a statement as saying that all mathematics is based on set theory. While it may be true, it may be too low a level of abstraction, and there are probably higher-order constructs or machinery that get closer to your actual domain of study or application (e.g. real numbers, functions, vectors).

I have mentioned downthread [0] a few characteristics that distinguish FP from other paradigms - namely, first-class functions and higher-order functions (the ability to pass functions to functions); referential transparency, "pure functions" without side effect, and equational reasoning (the ability to replace expressions with their value and keep the behaviour of the program identical); and a tendency to prefer parametric and ad-hoc polymorphism (generics and trait/typeclass bounds) to subtype polymorphism, the "inheritance" boogeyman of OOP.

Again as mentioned in [0] the "expression problem" [2] is a good model for discussing the ergonomics and tradeoffs of FP vs. OOP.

[0] https://news.ycombinator.com/item?id=41450851

[1] https://news.ycombinator.com/item?id=41452258

[2] https://wiki.c2.com/?ExpressionProblem

Re: What's functional programming all about? (2017)

#75

Earlier quoted context omitted.

I think the point of the article is to illustrate why "no side effect" is important.

While I agree, you can get all of that FP example from the article in say C++ by liberal const -usage. So is "const-y" C++ functional programming?

I think liberal const-usage breaks move optimizations so it's not used in practice.

Re: What's functional programming all about? (2017)

#76
post #9

Interesting how the post doesn't mention the word "side effect" once. To me, all of this could be summarized by "no side effect".

this is wrong! ocaml is a functional programming language with side effects.

To be fair, OCaml is multi-paradigm.

Re: What's functional programming all about? (2017)

#77
post #72
post #66

Earlier quoted context omitted.

Define what you mean by "everyone" -- there are times where the cost of immutability can be overwhelming, such as in high traffic systems with overly complex data structures which you are required to use because someone who should have known better insisted upon writing. (sorry, bitter personal experience) And yes, that is explicitly "modern" Java code written by a lead engineer and "java champion" in 2023.

Yes, I feel you. As I said, we often need to sacrifice immutability to performance, and that's ok. If they insisted using immutable structures in high-performance applications, then functional programming won't help anyway.

Hopefully we won't have to make that trade off for too long

https://www.microsoft.com/en-us/research/uploads/prod/2020/1...

Re: What's functional programming all about? (2017)

#78

As a programmer, I don't know if it's still relevant to make a strict separation between programming paradigms. You can use immutable types, pure functions, closures and so on in most languages. Conversely, you can define mutable types and imperative code in most functional programming languages. I'm always surprised reading comments on these topics, people saying they don't grasp FP. But don't we use higher-order fu…

Mainstream languages are not expression orientated like true FP languages are. Most people working in mainstream languages aren’t aware of the significance of this and wonder why FP seems awkward in their language, despite it having closures, some immutable types, etc.

One thing that struck me while learning nushell is that instead of:

    $ echo "hello world"
    hello world
I can do:

    $ "hello world"
    hello world
Is this an indication that it is expression oriented? (just checking that I've understood the phrase).

Re: What's functional programming all about? (2017)

#79
post #64

The article itself was well written, although I'd appreciate if the author was more "to the point" with the examples. FP never resonated with me and never fit the mental model I have for programming. I tried learning Prolog and Haskell a few times, and I never felt like I could reason about the code. This line from the article: "[..] With functional programming, whether in a typed language or not, it tends to be much…

I think I find a lot of FP code harder to both write and read (even code I've written myself), but when the FP code is written and compiles, it is much more likely to be correct. It's an annoying trade off, to be sure. With a language that makes writing FP code ergonomic and idiomatic, though, I'm usually going to choose to write that way. But even in languages where writing in an FP style is a chore (if it's even po…

>it is much more likely to be correct

It is not. FP programs measurably have at least as many defects as non-FP programs.

There is literally no reason to subject yourself and, worse, your users, to the garbage of FP.

Re: What's functional programming all about? (2017)

#80

Earlier quoted context omitted.

Mainstream languages are not expression orientated like true FP languages are. Most people working in mainstream languages aren’t aware of the significance of this and wonder why FP seems awkward in their language, despite it having closures, some immutable types, etc.

One thing that struck me while learning nushell is that instead of: $ echo "hello world" hello world I can do: $ "hello world" hello world Is this an indication that it is expression oriented? (just checking that I've understood the phrase).

This is the statement/expression distinction; `echo "hello world"` is a statement that, when evaluated, has the side effect of printing "hello world" to stdout (or wherever), and has no value (for the purposes of this example).

`"hello world"` is an expression with the literal string value "hello world"; your REPL probably prints out that expression's value once it is evaluated for convenience.

FWIW I'm not actually familiar with nushell and am speaking fairly generally about statements vs. expressions.

Post reply on HN