I would like to represent a circuit with the following behaviour in Haskell:
The circuit has two inputs :
- a data input
- and a switch input
and one output.
- When the switch =
True
then output t = input t - when switch =
False
, output t = output t-1
I need a way to represent the feedback loop, and the state.
I know there is several libraries providing abstractions for this sort of thing, but they look like magic to me.
Is there a simple way to model this?
Edit:
type Signal a = [a]
type Input = Signal Int
type Output = Signal Int
type State = Int
type Switch = Signal Bool
delay :: a -> Signal a -> Signal a
delay = (:)
circuit :: State -> Input -> Switch -> Output
circuit s (i:is) (True:bs) = i : circuit i is bs
circuit s (i:is) (False:bs) = s : circuit s is bs
I used streams to represent the signals, and I carry the state around explicitly in s. But what if I wanted to extract (look at) the elements in the output stream on the fly?
It looks like a problem that the State Monad would solve but I can't find a way to represent it.