# Using variables within patterns

**URL:** <https://uzu.lurk.org/t/using-variables-within-patterns/1119>\
**Category:** TidalCycles\
**Tags:** questions\
**Created:** [22 May 2020 17:45 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119 "2020-05-22T17:45:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanD](https://uzu.lurk.org/user_avatar/uzu.lurk.org/dand/32/484_2.png) [@DanD](https://uzu.lurk.org/u/DanD)\
**Post date:** [22 May 2020 17:45 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/1 "2020-05-22T17:45:04Z")

</div>

Apologies if this has been answered elsewhere, had a search but couldn't quite find what I was looking for. I'm trying to figure out how to use a variable in place of one part of pattern. I know that you can define a whole pattern in a variable like this:

```haskell
do
    let structPatt = "[t(< 2 1 3 1 >,4,< 0 2 1 >)]*4"
    d1 $ struct structPatt $ n "0 3 7 10" # s "superchip"

```

But what if you wanted to have just part of that pattern be a variable, so something like:

```haskell
d1 $ struct "[t(< VARIABLE_HERE!!! >,4,< 0 2 1 >)]*4" $ n "0 3 7 10" # s "superchip"

```

How would you define and use that variable? I've tried a bunch of different ways and all lead to some kind of error.

---

<div class="post-metadata">

**Author:** ![yaxu](https://uzu.lurk.org/user_avatar/uzu.lurk.org/yaxu/32/2_2.png) [@yaxu](https://uzu.lurk.org/u/yaxu)\
**Post date:** [22 May 2020 18:35 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/2 "2020-05-22T18:35:52Z")

</div>

Hi @DanD

There isn't a way to do this at the moment, unfortunately.

It's something we've looked at:

> <https://github.com/tidalcycles/Tidal/issues/530>
>
> A continuation of discussion on this PR by @bgold-cosmos : https://github.com/ti…dalcycles/Tidal/pull/528

I don't think it's too difficult to implement, but it hasn't come to fruition just yet..

---

<div class="post-metadata">

**Author:** ![yaxu](https://uzu.lurk.org/user_avatar/uzu.lurk.org/yaxu/32/2_2.png) [@yaxu](https://uzu.lurk.org/u/yaxu)\
**Post date:** [22 May 2020 23:35 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/3 "2020-05-22T23:35:15Z")

</div>

Ok I hacked something together, should be available in the next Tidal version later this week!

---

<div class="post-metadata">

**Author:** ![ben](https://uzu.lurk.org/user_avatar/uzu.lurk.org/ben/32/362_2.png) [@ben](https://uzu.lurk.org/u/ben)\
**Post date:** [23 May 2020 03:28 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/4 "2020-05-23T03:28:48Z")

</div>

oh wow! excited to see this 🙏

---

<div class="post-metadata">

**Author:** ![DanD](https://uzu.lurk.org/user_avatar/uzu.lurk.org/dand/32/484_2.png) [@DanD](https://uzu.lurk.org/u/DanD)\
**Post date:** [23 May 2020 13:59 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/5 "2020-05-23T13:59:38Z")

</div>

That's super awesome! I was happy with 'Sorry no can do' tbh 🙂 This could open up all sorts of interesting creative possibilities and make working with long complex patterns much easier. Thanks @yaxu

---

<div class="post-metadata">

**Author:** ![yaxu](https://uzu.lurk.org/user_avatar/uzu.lurk.org/yaxu/32/2_2.png) [@yaxu](https://uzu.lurk.org/u/yaxu)\
**Post date:** [23 May 2020 14:13 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/6 "2020-05-23T14:13:36Z")

</div>

This is working:

```haskell
setI "groove" $ iter 4 "0 3 [~ 2] 4"
setS "boop" $ chunk 4 (fast 2) "bd sd [~ bd sd]"

d1 $ n "^groove" # sound "cpu"
d2 $ s "^boop clap:4 ^boop bd*2"

-- + on-the-fly changes work:

setS "boop" $ slow 2 $ chunk 4 (fast 2) "gabba(3,8)"

```

I think this will open up some other unexpected possibilities too, including extra-easy MIDI input..

Should be able to get this released later!

---

<div class="post-metadata">

**Author:** ![ben](https://uzu.lurk.org/user_avatar/uzu.lurk.org/ben/32/362_2.png) [@ben](https://uzu.lurk.org/u/ben)\
**Post date:** [23 May 2020 15:10 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/7 "2020-05-23T15:10:01Z")

</div>

Yeah I’m really excited about the midi input possibilities.

---

<div class="post-metadata">

**Author:** ![heavy.lifting](https://uzu.lurk.org/user_avatar/uzu.lurk.org/heavy.lifting/32/150_2.png) [@heavy.lifting](https://uzu.lurk.org/u/heavy.lifting)\
**Post date:** [23 May 2020 21:57 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/8 "2020-05-23T21:57:55Z")

</div>

Very nice ::🙂

---

<div class="post-metadata">

**Author:** ![artheist](https://uzu.lurk.org/user_avatar/uzu.lurk.org/artheist/32/594_2.png) [@artheist](https://uzu.lurk.org/u/artheist)\
**Post date:** [10 June 2020 01:33 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/9 "2020-06-10T01:33:08Z")

</div>

This does not seems to work for me:

```haskell
setI "pulse" $ (cI 5 "21")

setI "step" $ (cI 16 "22")

d1 $ "bd(^pulse, ^step)" 

```

where 21 and 22 are two MIDI knobs on my controller.  
Also, it works fine with :

```haskell
 d1 $ sound "bd*16"
    # djf (cF 0.5 "21")

```

What I mean but does not work, it is taking the default value but values are not changing while moving the knobs

---

<div class="post-metadata">

**Author:** ![yaxu](https://uzu.lurk.org/user_avatar/uzu.lurk.org/yaxu/32/2_2.png) [@yaxu](https://uzu.lurk.org/u/yaxu)\
**Post date:** [21 August 2020 22:10 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/10 "2020-08-21T22:10:16Z")

</div>

Hmm I can't remember how this works, but it might be difficult to fix.

Howabout this?

> [@artheist](#):
>
> `d1 $ "bd(^21, ^22)" `

You could also do the mapping from numbers to names in supercollider.

---

<div class="post-metadata">

**Author:** ![artheist](https://uzu.lurk.org/user_avatar/uzu.lurk.org/artheist/32/594_2.png) [@artheist](https://uzu.lurk.org/u/artheist)\
**Post date:** [22 August 2020 13:57 UTC](https://uzu.lurk.org/t/using-variables-within-patterns/1119/11 "2020-08-22T13:57:17Z")

</div>

Yeah, this one is the same except there is no default value so no sound at all.

I could doing the translation in SC, but I feel that this is no the problem, since the value are received correctly on tidal side, as long as it is not in mininotation.

For example, this works :

```haskell
setI "drywet" $ (cI 5 "17")

 d1 $ sound "bd*16"
    # djf drywet

```
