# Iterative functions?

**URL:** <https://forum.toplap.org/t/iterative-functions/470>\
**Category:** TidalCycles\
**Created:** [29 April 2019 22:38 UTC](https://forum.toplap.org/t/iterative-functions/470 "2019-04-29T22:38:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![crashingbooth](https://forum.toplap.org/user_avatar/forum.toplap.org/crashingbooth/32/601_2.png) [@crashingbooth](https://forum.toplap.org/u/crashingbooth)\
**Post date:** [29 April 2019 22:38 UTC](https://forum.toplap.org/t/iterative-functions/470/1 "2019-04-29T22:38:24Z")

</div>

Apologies because:

- I’m fairly new to Tidal/Haskell/functional programming
- I’m not sure if this is the right place to post

But I’m interested in working with iterative functions (things like cellular automata, markov chains and so on). I can do this easily enough in Haskell with a recursive function, but I’m not sure how to best make this playable in Tidal. I can write a simple function that, for example, takes a gain pattern as input and returns a different pattern - but is there a way to get that outputted pattern to act as the input for the next repetition (and so on ad infinitum)? I’m struggling here because intuitively I keep wanting to use something mutable to keep state.

I’m sure that there must be multiple ways of doing this - it would be really helpful to hear a few ideas.  
Thanks

---

<div class="post-metadata">

**Author:** ![xinniw](https://forum.toplap.org/user_avatar/forum.toplap.org/xinniw/32/274_2.png) [@xinniw](https://forum.toplap.org/u/xinniw)\
**Post date:** [8 May 2019 13:58 UTC](https://forum.toplap.org/t/iterative-functions/470/2 "2019-05-08T13:58:10Z")

</div>

I have a similar question and ended up figuring something out. The catch is the ‘ad infinitum’ part. If you define an iteration depth you can compute the whole pattern (which may be composed of iterative modifications to the base pattern) all at once and output that.

For example, here is the source code for `soak` which repeatedly applys some transformation and glues the result to the previous result.

```auto
-- d1 $ soak 4 (iter 4) $ s "{bd(3,8), sn(2,5,3)}"
soak :: Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
soak depth f pattern = cat $ take depth $ iterate f pattern

```

`iterate` applies some function over and over and returns an ‘infinite’ list of the results. (ie. for some function f and some data `x : [x, f(x), f(f(x)), f(f(f(x))), ....]`) `take` then truncates this list to avoid returning an infinite pattern. Finally, `cat` glues the resulting patterns together.

Here is a similar idea that attempts to combine the last result with the next result

```auto
-- d1 $ note (scale "minor" $ snowball 4 (+) (slow 2 . rev) "0 3 . 2 4 6") # s "gtr"
snowball :: (Pattern a -> Pattern a -> Pattern a) -> (Pattern a -> Pattern a) -> Int -> Pattern a -> Pattern a
snowball depth combinationFunction f pattern = cat $ take depth $ scanl combinationFunction pattern $ iterate f pattern

```

I would love to be able to have the transform operate on the output of last cycle forever and ever, like you were describing. That I really don’t know how to do functionally.

---

<div class="post-metadata">

**Author:** ![crashingbooth](https://forum.toplap.org/user_avatar/forum.toplap.org/crashingbooth/32/601_2.png) [@crashingbooth](https://forum.toplap.org/u/crashingbooth)\
**Post date:** [9 May 2019 00:52 UTC](https://forum.toplap.org/t/iterative-functions/470/3 "2019-05-09T00:52:16Z")

</div>

Thanks so much - that is really helpful code to look at.

This is the kind of approach I’ve been using these last few days (with some success), but I didn’t know about the `iterate` function. Actually, I’m feeling a little stupid because I encountered a namespace conflict with `iterate` right as I was finishing off writing what was essentially my own version of it. But I didn’t look into it at the time. I was iterating through`[Int]` rows which I transformed into `String` then made patterns via `parseBP_E` (which I also haven’t looked deeply into yet), then queued them up together with `slowcat`.

I can basically pre-calculate an arbitrarily big number of iterations of patterns and then play them through sequentially. The effect is right, but if feels somehow at odds with making music generatively.

I wonder if there is some way to do it in real time, so to speak. It would be great to be able to make music that way in Tidal.

---

<div class="post-metadata">

**Author:** ![xinniw](https://forum.toplap.org/user_avatar/forum.toplap.org/xinniw/32/274_2.png) [@xinniw](https://forum.toplap.org/u/xinniw)\
**Post date:** [9 May 2019 02:37 UTC](https://forum.toplap.org/t/iterative-functions/470/4 "2019-05-09T02:37:27Z")

</div>

I think there must be some way to do it. Haskell is a general purpose language and I’m pretty sure I’ve even seen some people doing markov chains in tidal before.

There is a concept that I’ve seen andrew sorensen mention called “tempo recursion” where state is passed from iteration to iteration with a delay. I don’t know if its possible to get state from one cycle to another but that could function as a kind of tempo recursion.

EDIT:

Here is what I was thinking of for the markov chains: [http://blog.tidalcycles.org/developing-a-markov-chain/](http://blog.tidalcycles.org/developing-a-markov-chain/)

---

<div class="post-metadata">

**Author:** ![kindohm](https://forum.toplap.org/letter_avatar_proxy/v4/letter/k/e79b87/32.png) [@kindohm](https://forum.toplap.org/u/kindohm)\
**Post date:** [28 May 2019 13:00 UTC](https://forum.toplap.org/t/iterative-functions/470/6 "2019-05-28T13:00:50Z")

</div>

Also check out the `perlin` function, which is like `rand` but produces related random values.

---

<div class="post-metadata">

**Author:** ![xinniw](https://forum.toplap.org/user_avatar/forum.toplap.org/xinniw/32/274_2.png) [@xinniw](https://forum.toplap.org/u/xinniw)\
**Post date:** [29 May 2019 01:38 UTC](https://forum.toplap.org/t/iterative-functions/470/7 "2019-05-29T01:38:38Z")

</div>

oh interesting! that neat how it works inside. doing things functionally still makes my brain hurt. That gives me some more ideas on how to make the next cycle based on the last cycle.
