Timeline for Can functional programming used for solving problems which require randomness?
Current License: CC BY-SA 3.0
31 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 4, 2018 at 17:18 | comment | added | Steve | @pjc50, there is a monad for it, but there is not a function for it, and anything program that uses an I/O monad forfeits its functionalness (and the guarantees that came with it). I'm not trying to compare functional languages adversely - I'm just exploring certain lesser-considered aspects of them (including the fact that an I/O monad is not a function, and it also depends on an imperative order of execution/evaluation, having dire implications for concurrent evaluation of the program, whereas the order of evaluating pure functions depends only on analysing the availability of inputs). | |
Jan 4, 2018 at 16:59 | comment | added | pjc50 | @Steve well, if you want IO, there's a monad for that. But my point was that setting up conditions where one program is allowed input randomness and the other isn't is setting up a lopsided comparison. | |
Jan 4, 2018 at 16:57 | comment | added | Steve | @pjc50, the difference is that an imperative program can (pseudo-)randomise it's own output regardless of its starting conditions (because it operates under fewer constraints) - it can acquire new input during its operation. A (true) functional program cannot - its output is predictable before it begins (as soon as its inputs are known), and repeatable. That is why functional programs have a smaller scope of use (than imperative programs) and are typically short-running affairs, because frequently it is desirable for a program to interact with its environment and take new input as it goes. | |
Jan 4, 2018 at 11:51 | history | protected | gnat | ||
Jan 4, 2018 at 9:57 | comment | added | pjc50 | This does feel like a "trick question"; under the conditions specified (no IO, deterministic machine execution), you can't implement "true" randomness in an imperative language either. | |
Jan 4, 2018 at 9:31 | answer | added | libeako | timeline score: 1 | |
Jan 3, 2018 at 23:12 | answer | added | Jörg W Mittag | timeline score: 4 | |
Jan 3, 2018 at 13:43 | history | reopened |
Doc Brown maple_shaft♦ |
||
Jan 3, 2018 at 12:12 | history | edited | Vicky | CC BY-SA 3.0 |
Asking Haskell & Clojure programmers to demonstrate random number function with a working code snippet.
|
Jan 3, 2018 at 9:39 | history | edited | Vicky |
adding Clojure and Haskell tags.
|
|
Jan 3, 2018 at 9:13 | history | edited | Doc Brown | CC BY-SA 3.0 |
added 13 characters in body
|
Jan 3, 2018 at 8:59 | review | Reopen votes | |||
Jan 3, 2018 at 13:49 | |||||
Jan 3, 2018 at 8:42 | history | edited | Doc Brown | CC BY-SA 3.0 |
Modified the question in a way to make it different to the one it links to.
|
Jan 3, 2018 at 8:07 | comment | added | Steve | @Jules, if a function is "pure" (i.e. its output is a function of its input), then its output ceases to be random (i.e. it's output is functional). That circle cannot be squared. I'm not railing against functional languages - I'm simply pointing out that any functionalism of a value detracts from its randomness. Any functional program that is said to be random, must merely shift the problem (and the requirement for non-functionalness) outside of the scope of the program, by operating deterministically on its non-deterministic inputs. | |
Jan 3, 2018 at 8:02 | history | closed |
Doc Brown kevin cline Jörg W Mittag gnat Jules |
Duplicate of How do functional languages handle random numbers? | |
Jan 3, 2018 at 7:59 | comment | added | Steve | @rwong, don't get me wrong, I'm not saying lookup tables would never be acceptable for a particular use case. Pseudo-random, or even just obscurely deterministic, operation is often perfectly sufficient, or even desirable. I'm just addressing the OP in terms of first principles, that "randomness" and "functional-ness" are two fundamentally opposed and irreconcilable concepts. | |
Jan 3, 2018 at 7:54 | comment | added | Jules | @Steve - the use of a monad to provide a sequence of random numbers (potentially initialized via the use of IO or some other externally-sourced seed) as shown in the accepted answer to that question is not "non-functional" in any way. Every line of code in that answer consists only of deterministic, pure functions that always give the same result for the arguments passed to them. The trick is in arranging for your non-deterministic values to be passed to a user-specified function, rather than returned from a system-called one. | |
Jan 3, 2018 at 7:41 | comment | added | rwong | @Steve: Your response is correct, but it is a major topic in reproducible software building, execution, and results. The use of lookup table is what allows deterministic results. Executions using different lookup tables are assembled into an "ensemble" so that ensemble statistics can be extracted from all of them. | |
Jan 3, 2018 at 7:38 | history | edited | Vicky | CC BY-SA 3.0 |
added 104 characters in body; edited title
|
Jan 3, 2018 at 7:36 | comment | added | Steve |
@JörgWMittag, to be fair to the OP, I don't think that link is clear enough that the answer to her question "how to functional languages handle random numbers", is that "they don't". Functions don't handle non-determinism, and "functional languages" handle non-determinism essentially by being non-functional (that is, by stepping outside the functional paradigm and employing non-functional code) - and so-called functional languages actually allow non-functional code (and non-functional programs) to be written. In the same way that "structured languages" permit unstructured code (e.g. GOTOs ).
|
|
Jan 3, 2018 at 7:35 | comment | added | Vicky | @DocBrown : I wanted a fresh thread explaining the logic in a simpler way to people like us coming from imperative background. The earlier thread doesn't account for that. It assumes the asker has a functional background. Kindly take a look at my edited question. It is not a dupe. | |
Jan 3, 2018 at 7:27 | comment | added | Steve | @rwong, basically what you are describing is a lookup table. But eventually, for a table of non-infinite length, the values must repeat in a fixed sequence (and thus the function is not random, even if the lookup table was randomly generated). Also, usually the point of a random number generator is to avoid the possibility that anyone with a knowledge of the inputs can calculate the outputs - the very existence of a lookup table that links inputs to outputs violates the requirement of randomness. | |
Jan 3, 2018 at 7:21 | answer | added | Steve | timeline score: 3 | |
Jan 3, 2018 at 7:19 | comment | added | rwong | Basically you need to enrich your understanding in order to understand answers to your question. | |
Jan 3, 2018 at 7:19 | comment | added | rwong | Another option that does not depend on having a pre-generated list as initialization is to choose a cryptographic hash generating function, and use the hash of the "invoke number" as the random value. | |
Jan 3, 2018 at 7:19 | review | Close votes | |||
S Jan 3, 2018 at 8:05 | |||||
Jan 3, 2018 at 7:18 | comment | added | rwong | The code (algorithm) that requires a random source will now need to be rewritten, so that each time it requires a random value, it needs to pass in a "invoke number". An example is, say, incrementing integers, 0, 1, 2,..., where the "fixed random function" will simply look up the corresponding pre-generated random number from the list. This ensures that, given the same "invoke number", the returned pre-generated number is the same. This removes the dependence on order of evaluation. | |
Jan 3, 2018 at 7:14 | comment | added | rwong | Several options are: (1) make it an impure function that interacts with the outside and/or maintain (use and update) its own state with each call. (2) Use an external tool to pre-generate a huge but finite-size stream of random data, then allow the "random function" to take this stream and feed its values one-by-one to its consumers. In the simplest case, consider zip-with the (fixed, pre-generated) random stream with another input stream. | |
Jan 3, 2018 at 6:55 | comment | added | Philip Kendall | How does your hypothetical functional language handle I/O? | |
Jan 3, 2018 at 6:37 | review | First posts | |||
S Jan 3, 2018 at 8:05 | |||||
Jan 3, 2018 at 6:37 | history | asked | Vicky | CC BY-SA 3.0 |