On 28/01/2024 21:40, Lawrence D'Oliveiro wrote:
On Sun, 28 Jan 2024 12:35:00 +0100, David Brown wrote:
On 27/01/2024 21:49, Lawrence D'Oliveiro wrote:
The dirty little secret of pure-functional programming is that I/O
cannot be treated in a purely functional fashion.
It can - you just have to have the IO world as an input and an output to
your function.
You mean, carry around multiple copies of the entire Universe in
individual variables?
No, you only need one copy at a time :-) And you only actually need a
bit of the universe, covering your input and output streams.
Another option is that your code does not actually do any IO, but
returns a function that can be applied to the world to give the results
you want.
Real functional programming languages have smarter ways of doing this in
an efficient way. The functions may be pure as you see them in code,
but the generated results do IO just like any other generated code in
other languages. This gives you the advantages of purity at the source
level - it's far easier to have mathematical proofs of the code's
correctness, multi-threading or asynchronous coding is easy because you
have no state, therefore no shared state, therefore no race conditions.
But the end result is efficient - perhaps not "well-written C"
efficient, but close enough to be of practical use in the real world.
The key challenge is that you have to understand monads!
A much simpler idea in a similar vein is that functional programming
languages don't have iterative loops - you use lists or recursion to
express repetition. In C, having a recursive function that works
through a linked list is massively inefficient compared to a for loop,
in both run-time and stack space. In a functional programming language,
lists and recursion make it simple and clear to express the code - and
the compiler turns it all into an iterative loop in the generated code.
(Facebook uses Haskell for their filters and other data handling. You
may not think much about Facebook's algorithms, but it shows that pure functional programming works fine and efficiently for big IO
applications. Major mobile phone systems are programmed in Erlang,
which is a functional programming language, albeit an impure one which
can "cheat".)
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)