Created
March 25, 2017 15:45
-
-
Save useronym/6aef673530f950149c2464a781fb954a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data State = St Int ((Int, State) -> (Int, State)) | |
unfold2 :: (State, State) -> [Int] | |
unfold2 (St x f, St y g) = let (x', St y' g') = f (x, St y g) in | |
x : (unfold2 (St y' g', St x' f)) | |
-- Some examples. | |
-- f produces even numbers, g produces odd. The functions do not modify each other. | |
count = unfold2 (St 0 f_g, St 1 f_g) | |
where f_g (x, s) = (x + 2, s) | |
-- Produce Fibonacci numbers. Again, functions do not modify each other. | |
fib = unfold2 (St 0 f, St 1 g) | |
where f (x, St y g') = (x + y, St y g') | |
g (y, St x f') = (y + x, St x f') | |
-- f kills g by overwriting it and its state with its own data. | |
-- Then they keep overwriting each other again and again. | |
kill = unfold2 (St 1 f, St 1 g) | |
where f (x, St y g') = let x' = x + 1 in (x', St x' f) | |
g _ = undefined | |
-- Both functions just want to increment, but they keep messing with each other | |
-- in the sense that, uhh… "the other function sure lets me increment, but then it | |
-- subtracts all I had in my state before incrementing!" | |
-- This keeps "piling on", so that in each step the funs get subtracted the sum of | |
-- all theor previous states, or something like that. | |
-- This generates a repeating sequence: [0,1,1,1,1,0,0,-1,-1,-1,-1,0,…] | |
fight = unfold2 (St 0 f, St 1 g) | |
where f (x, St y g') = (x + 1, St y ((\(y', s) -> (y'-y, s)) . g')) | |
g (y, St x f') = (y + 1, St x ((\(x', s) -> (x'-x, s)) . f')) | |
-- Can we get a seemingly trivial setup, such that f and g end up generating | |
-- some pseudo-random / chaotic sequence? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment