3 Replies - 997 Views - Last Post: 26 April 2014 - 02:26 PM

#1 krstefan11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-March 14

Why is functional purity desirable?

Posted 26 April 2014 - 01:49 PM

I've only ever used procedural languages before, but functional languages seem intriguing. Functional programming seems like a powerful paradigm, but I haven't been able to see the point of "pure" functional languages like Haskell. I know that in pure functional languages, no side effects or mutable state are allowed, and this seems kind of impractical and misguided to me. IO is inherently based on state and side effects, and so are simulations that have to evolve their state over time. Sure, you can have globally evolving simulations by passing global state in and out of functions, and you can implement a global loop with a recursive function. But for all intents and purposes, your code still has mutable state, it's just viewed through a slightly different lens. So why not just allow side effects in the first place?

I've also been reading about monads in Haskell. As I understand it ( and I understand it poorly, so please correct me if I'm wrong), monads allow you to easily represent state and side effects, and actually hide the functional implementation of those features, so you don't have to worry about explicitly passing global state into functions. That makes all the fuss about functional purity seem pretty silly. Why go to all the trouble of representing state in a functional way when, in the end, your're just hiding the functional nature of the program and have simply re-implemented side effects on top of a functional language?

Is This A Good Question/Topic? 0
  • +

Replies To: Why is functional purity desirable?

#2 krstefan11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-March 14

Re: Why is functional purity desirable?

Posted 26 April 2014 - 02:02 PM

Oops, this should have been in the functional languages forum. Maybe a mod can move it.
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Why is functional purity desirable?

Posted 26 April 2014 - 02:15 PM

As I see it could be in both.

Functional Purity is just relying on the input arguments to the function and not use or modify anything to produce the result.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Why is functional purity desirable?

Posted 26 April 2014 - 02:26 PM

Functional purity is required for laziness. If you have unrestricted side-effects in a lazy language, it will be very hard to make any program do what you want and your programs would be very prone to bugs. That's why Haskell needs to enforce functional purity.

Enforced purity also enables the compiler to optimize more liberally, but I'd say that's a secondary concern. And of course pure functions are more easily reasoned about and forcing every function to be pure prevents a situation where you think a function is pure when in reality it's not¹.

Note that most strict languages do not enforce functional purity and simply encourage you to avoid side-effects where possible. Without laziness that tends to work perfectly fine.

¹ That said that does not appear to be a big problem in practice. As some whose programmed a lot in impure functional programming language, it's usually pretty clear when a function is pure and when it is not (as long as you're only using functions from the standard library or written by people who stick to functional programming practice).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1