1

I wanted to try a mental experiment of creating a stateful function by using only pure ones, without using any sorts of assignment/monads and such. For instance, a function resembling a RS-flipflop, which has Set and Reset inputs:

ff(1,0) -> 1 ; SET
ff(0,0) -> 1 ; just output current state
ff(0,1) -> 0 ; RESET
ff(0,0) -> 0 ; whoops, a side-effect!

In electronics such a flipflop (or any stateful circuit) is implemented by sending output of a circuit back to the input, i.e:
enter image description here

So, I am thinking that some sort of recursion of functions can create stateful functions, right? But how to handle the thing, that it will be infinite?

artemonster
  • 744
  • 4
  • 27
  • 1
    Unless you have a definition of "stateful function" which doesn't amount to "impure function", what you seek is impossible. You seem to want to find a way to violate referential transparency with pure functions, – John Coleman May 11 '16 at 15:36
  • 2
    You can write a function that takes a list of inputs and produces a list of outputs. Which is effectively a kind of an event stream in *functional reactive programming* then. – Bergi May 11 '16 at 16:36
  • A function does have state and making this you will be implementing what is commonly known as a monad, however since you have required not to use monads you probably cannot use lexical closures as state and the task turns impossible. – Sylwester May 11 '16 at 23:20
  • I kinda hoped someone would elaborate more on Bergi's answer and this SICP snippet: "In this section, we explore an alternative approach to modeling state, based on data structures called streams". You only need a pure lambda calculus (with no assignment) to implement lazy streams, right? So why this all is impossible, according to John and Sylwester? – artemonster May 12 '16 at 06:30

0 Answers0