# Week 3 lesson 1 - exploring the 'every' function, including tackling the meaning of '$'

**URL:** <https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502>\
**Category:** Week 3 - every, $, working with longer samples\
**Created:** [26 April 2020 19:44 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502 "2020-04-26T19:44:47Z")\
**Posts on this page:** 18\
**Page:** 1

<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:** [26 April 2020 19:44 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/1 "2020-04-26T19:44:48Z")

</div>

Before getting on to working with longer samples, here's something about the `every` function. It's a nice clear example of how functions work, and gives us the opportunity to start to get a feel for how parenthesis and `$` works. I also go through how to add an effect as a function.

I had a lot of problems with corrupted subtitle files which I won't go into.. and only after editing the subtitles noticed that my friend the vertical grey oblong decided to join me in the video. They're not really in the way so I decided not to reshoot it all, hope they don't get too distracting!

[![](https://img.youtube.com/vi/2I74QEdXzBY/maxresdefault.jpg "[TidalClub] Week 3 lesson 1 - exploring the 'every' function, including tackling the meaning of '$'") ](https://www.youtube.com/watch?v=2I74QEdXzBY)

Here's the worksheet:

```haskell

-- every

-- 'every' is one of a family of Tidal functions, that takes another
-- function as one of its inputs.

-- Let's say we had a simple pattern like this:
d1 $ sound "bd sd ~ cp"

-- ... and we wanted to speed it up like this:
d1 $ fast 2 $ sound "bd sd ~ cp"

-- ... but only one cycle out of three.

-- Here's how we'd use 'every' to do that:
d1 $ every 3 (fast 2) $ sound "bd sd ~ cp"

-- You can read this as "every 3rd cycle, make 'sound "bd sd ~ cp"',
-- go faster by a factor of two."

-- We'll take this apart to work out why we sometimes use (), and
-- sometimes '$' later. First, lets look at more, practical examples
-- of using 'every'.

-- We can use every with any function that takes single pattern as
-- input (and returns a transformed version as output). For example,
-- we can use 'hurry' instead of fast:
d1 $ every 3 (hurry 2) $ sound "bd sd [~ bd] [cp bd*2]"

-- Or use 'rev':
d1 $ every 3 (rev) $ sound "bd sd [~ bd] [cp bd*2]"

-- Because 'rev' is a single word, we don't actually need to put it in
-- parenthesis:
d1 $ every 3 rev $ sound "bd sd [~ bd] [cp bd*2]"

-- Here's a trick with using effects as functions..
-- Lets look at this:
d1 $ sound "bd sd [~ bd] [cp bd*2]"
   # squiz "5"

-- We can treat the '# speed 5' bit as a function. If you think about
-- it, it does something to a pattern, just like 'fast 2' does.

-- So.. does this work?
d1 $ every 3 (# squiz 5) $ sound "bd sd [~ bd] [cp bd*2]"

-- Yes it does!

-- You can also add more than one 'every' manipulation, giving them
-- different periods for their first input, to create longer form
-- variety:
d1 $ every 3 (# squiz 5) $ sound "bd sd [~ bd] [cp bd*2]"

d1 $ every 2 (hurry 2) $ every 3 (# squiz 5) $ sound "bd sd [~ bd] [cp bd*2]"

-- keep going..
d1 $ every 4 rev $ every 2 (hurry 2) $ every 3 (# squiz 5)
   $ sound "bd sd [~ bd] [cp bd*2]"

-- In Tidal, the pattern that a function is manipulating is generally
-- its final input, which makes it easy to 'chain together' functions
-- like this.

-- Ok as promised, lets go back to our original, simple example:
d1 $ every 3 (fast 2) $ sound "bd sd ~ cp"

-- Lets go through the three 'inputs' (also sometimes called
-- 'parameters' or 'arguments') for every.

-- [a] 3 - how often a function is applied
-- [b] fast 2 - the function that is applied
-- [c] sound "bd sd ~ cp" - the pattern that it's applied to.

-- Looking again at this pattern, you can see that the inputs are
-- given in three different ways:
d1 $ every 3 (fast 2) $ sound "bd sd ~ cp"

-- '3' is just on its own. It's a single number so tidal has no
-- problem knowing it's a single input.

-- 'fast 2' is in parenthesis '(fast 2)'. Then the word 'fast' and
-- number '2' are grouped together into a function, _before_ being
-- passed to 'every' as its second input.

-- 'sound "bd sd ~ cp"' has $ in front. We *could* have done this
-- instead:
d1 $ every 3 (fast 2) (sound "bd sd ~ cp")

-- That works fine, but '$' does the same kind of job. It passes
-- what's on its left, to the function on its right, as a single
-- parameter. '$' has really low priority, which means everything on
-- its right is worked out first before being passed to the left.
d1 $ every 3 (fast 2) $ sound "bd sd ~ cp"

-- This saves you from having to match up ( and ) around a function's
-- final input. It doesn't work with anything other than the final
-- input, so unfortunately this _doesn't_ work

d1 $ every 3 $ fast 2 $ sound "bd sd ~ cp"

-- The above would work out 'fast 2 $ sound "bd sd ~ cp"' first, and
-- would then try to pass that to 'every' as its second parameter,
-- which doesn't make sense to tidal, so it returns an error.

-- Note that when Tidal makes an error, if there was already a
-- pattern running, it will keep that going. If you're live coding
-- in front of an audience, you probably don't want an error to
-- result in silence!

```

**Next lesson:**

> [@Week 3 lesson 2 - cut vs legato](https://club.tidalcycles.org/t/week-3-lesson-2-cut-vs-legato/515):
>
> Hi [@cycle0](https://uzu.lurk.org/groups/cycle0)! Here's the first of three videos sharing techniques for dealing with longer samples, this time looking at the 'cut' and 'legato' control patterns, and the difference between them. [https://www.youtube.com/watch?v=dQPmE1WaD1k](https://www.youtube.com/watch?v=dQPmE1WaD1k) Here's a couple of examples to play with. Note what happens to the 'bev' sample when you hush and there's nothing to 'cut' it.. d1 $ jux rev $ speed "\<1 0.5 0.75\>(\<3 5\>,8)" # sound "bev" # cut 1 # room 0.4 # sz 0.9 # gain 1.3 d2 $ jux rev $ sound "sax(3,8)"…

---

<div class="post-metadata">

**Author:** ![tedthetrumpet](https://uzu.lurk.org/user_avatar/uzu.lurk.org/tedthetrumpet/32/39_2.png) [@tedthetrumpet](https://uzu.lurk.org/u/tedthetrumpet)\
**Post date:** [27 April 2020 19:29 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/2 "2020-04-27T19:29:06Z")

</div>

That's a really clear and illuminating video Alex, thanks. I feel like I now 98% understand things I understood about 66.6% before!

---

<div class="post-metadata">

**Author:** ![cleary](https://uzu.lurk.org/user_avatar/uzu.lurk.org/cleary/32/177_2.png) [@cleary](https://uzu.lurk.org/u/cleary)\
**Post date:** [28 April 2020 20:32 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/3 "2020-04-28T20:32:36Z")

</div>

I liked that one too 23/66 for my percentages @tedthetrumpet 😉

@yaxu I would still call it snappy, not because of the time (13m is still a good length imo) but because you covered the content in exactly the level of detail required (imo of course!)

Thanks! 😃

---

<div class="post-metadata">

**Author:** ![gnozo](https://uzu.lurk.org/user_avatar/uzu.lurk.org/gnozo/32/100_2.png) [@gnozo](https://uzu.lurk.org/u/gnozo)\
**Post date:** [30 April 2020 01:20 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/4 "2020-04-30T01:20:23Z")

</div>

very cool. it becomes harder and harder to keep count of what's happening, which is funny and tricky in the case of weird rhythms and so on. i think i need to go back to the first lesson and write down everything you teach, i feel like i already forgot a lot!

---

<div class="post-metadata">

**Author:** ![ganz](https://uzu.lurk.org/user_avatar/uzu.lurk.org/ganz/32/64_2.png) [@ganz](https://uzu.lurk.org/u/ganz)\
**Post date:** [30 April 2020 10:15 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/5 "2020-04-30T10:15:54Z")

</div>

> [@yaxu](#):
>
> d1 $ every 3 (rev) $ sound "bd sd [~ bd] [cp bd\*2]"

Great lesson. Thanks for this again. But there is one thing, I don't understand:

If this is possible:  
d1 $ every 3 (rev) $ sound "bd sd [~ bd] [cp bd\*2]"

Why then doesn't work this like with hurry and probably:  
d1 $ every 3 (rev 2) $ sound "bd sd [~ bd] [cp bd\*2]"

Is it because otherwise "rev 2" would revert the reverted?```

---

<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:** [30 April 2020 10:33 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/6 "2020-04-30T10:33:13Z")

</div>

Hi @ganz,

`hurry` and `rev` are different 'types' of function.

`hurry` needs two inputs - the speed factor (the amount you're 'hurrying' by) and the pattern you're hurrying.

`rev` only needs one input - the pattern you're reversing.

Once you give `hurry` its first input, that speed factor, it only has one input left - the pattern to hurry. So you can say that `hurry 2` is of the same 'type' as `rev`.. Both take a pattern as input and return a transformed pattern as output.. and that's the type of function that `every` wants for its second input.

Here's the three inputs for `every` again:  
`every (number of cycles) (function that takes pattern as input, and returns pattern as output) (the pattern to work on)`

.. and then its output is the transformed pattern.

You can actually ask haskell what type of thing a function or value is with `:t`. For example:

```haskell
:t rev
rev :: Pattern a -> Pattern a
:t hurry
hurry :: Pattern Rational -> ControlPattern -> ControlPattern
:t hurry 2
hurry 2 :: ControlPattern -> ControlPattern

```

You can see that `rev` and `hurry 2` are similar, but `rev` deals with `Pattern a`s and `hurry 2` with `ControlPattern`s. `Pattern a` means a pattern of _any type_, so `rev` is more general than `hurry 2` - it can reverse patterns of numbers (such as `"1 2 3"`), patterns of words (such as `"bd bd"), or patterns of superdirt controls (such as `sound "bd bd"`). `hurry 2` _only_ works with patterns of superdirt controls.

Does that make some sense?

---

<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:** [30 April 2020 10:41 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/7 "2020-04-30T10:41:21Z")

</div>

We can also look at the type for `every`:

```haskell
:t every
every :: Pattern Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a

```

You can compare this with my description of it earlier.. Let me label the different inputs and outputs so you can compare:

- `every ([a]number of cycles) ([b]function that takes pattern as input, and returns pattern as output) ([c]the pattern to work on)` .. and then returns `[d]the transformed pattern`.
- `every :: [a]Pattern Int -> [b](Pattern a -> Pattern a) -> [c]Pattern a -> [d]Pattern a`

This can get a bit high minded, but you can understand how functions work just from working with examples, it comes with practice. If you want to look into the underlying principles though, then "partial application" and "currying" are good search terms.

---

<div class="post-metadata">

**Author:** ![1528DD](https://uzu.lurk.org/user_avatar/uzu.lurk.org/1528dd/32/70_2.png) [@1528DD](https://uzu.lurk.org/u/1528DD)\
**Post date:** [4 May 2020 21:19 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/8 "2020-05-04T21:19:48Z")

</div>

I just wanted to add that you can write

```
d1 $ every 2 (hurry 2) $ every 2 (# squiz 2) $ "bd sd"

```

more compact like this:

```
d1 $ every 2 ((hurry 2).(# squiz 2)) $ "bd sd"

```

If you are only using functions like fast, slow, striate, hurry, stut (functions that don't need a # inside the every-function) you can omit the brackets

```
d1 $ every 2 (hurry 2 . striate 16) $ "bd sd"

```

You can do this with as many functions as you like

```
d1 $ every 2 (hurry 2 . striate 16 . stut 4 0.5 0.3) $ "bd sd"
```

---

<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:** [4 May 2020 21:45 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/9 "2020-05-04T21:45:33Z")

</div>

Yep indeed! If you're feeling used to `#` and `$` and up for a challenge, then `.` is a good one to get your head around. It's a general Haskell thing, but in Tidal it's useful for putting two pattern-transforming functions in the place of one.

---

<div class="post-metadata">

**Author:** ![ravelite](https://uzu.lurk.org/user_avatar/uzu.lurk.org/ravelite/32/223_2.png) [@ravelite](https://uzu.lurk.org/u/ravelite)\
**Post date:** [5 May 2020 08:24 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/10 "2020-05-05T08:24:23Z")

</div>

This was awesome. Learning about the functionized # was a key takeaway for me.

---

<div class="post-metadata">

**Author:** ![schmolmo](https://uzu.lurk.org/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@schmolmo](https://uzu.lurk.org/u/schmolmo)\
**Post date:** [9 May 2020 07:31 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/11 "2020-05-09T07:31:11Z")

</div>

hey there,

thanks for the explanation! just to be sure i got it right is `d1` also a function that gets passed a pattern?

moritz

---

<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:** [9 May 2020 10:29 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/12 "2020-05-09T10:29:06Z")

</div>

Yes that's right! `d1` takes a pattern as input, and doesn't really have an output, apart from the 'action' of using the pattern to schedule events to send to superdirt.

---

<div class="post-metadata">

**Author:** ![cyberik](https://uzu.lurk.org/user_avatar/uzu.lurk.org/cyberik/32/1225_2.png) [@cyberik](https://uzu.lurk.org/u/cyberik)\
**Post date:** [28 September 2020 19:20 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/13 "2020-09-28T19:20:10Z")

</div>

Hello Yaxu, sorry for the delay but I'm studying the course now and not in live. It's very clear but I need an explanation on why in every we are using direct function like (rev) (hurry 2) (speed 2) and for crush; we need to put # on left (# crush 2). Even if crush is returning a Control Pattern like the others functions.

---

<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:** [29 September 2020 11:57 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/14 "2020-09-29T11:57:55Z")

</div>

Hi, the delay is no problem, some people are still just joining the course and I'm happy to answer questions any time.

This is a bit tricky.

Firstly, it's true that `rev` takes a pattern as input, and returns a new version of that pattern, with each cycle reversed. This will work with any kind of pattern, whether it's a pattern of numbers or a control pattern. It will just return a pattern of the same type as you give it.

`crush` and `speed` work a bit differently. They both take a pattern of numbers as input, and return a control pattern as output. So `crush 2` is a control pattern. So is `speed 2`.

That's the difference - `rev` is a function that works on patterns, `crush 2` is itself just a control pattern.

For its second input, `every` needs a function that works on patterns, like rev. `crush 2` isn't a function that works on patterns, it is itself a kind of pattern.

Ok so we want to give `every` a function that applies the `crush 2` control pattern to another pattern, but `every 2 (crush 2) $ sound "bd sn"` doesn't work. What is the function that combines one control pattern to another?

The answer is `#`. When we write `sound "bd sn" # crush 2`, it's the `#` that is combining the two control patterns `sound "bd sn"` and `crush 2`.

so `every 2 (# crush 2) $ sound "bd sn"` is what we want. It says, every second cycle (`every 2`), combine (`#`) a crush control pattern (`crush 2`) with the base pattern (`sound "bd sn"`)

_(remember that `$` here is just making sure that what is on its right is worked out in full before being passed to the function on it's left. We could also do `every 2 (# crush 2) (sound "bd sn")` for this.)_

Does that help?

---

<div class="post-metadata">

**Author:** ![cyberik](https://uzu.lurk.org/user_avatar/uzu.lurk.org/cyberik/32/1225_2.png) [@cyberik](https://uzu.lurk.org/u/cyberik)\
**Post date:** [29 September 2020 16:53 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/15 "2020-09-29T16:53:45Z")

</div>

Ok thanks @yaxu . It's very clear now. I reformulate to test if i have well understood. (fast 2) , (slow 2) , (hurry 2) are function that take a pattern/control pattern in input and return a pattern/control pattern, but (crush 2) or (speed 2) are control pattern only. So we need # to get the sound pattern on the right, combine it with the control pattern and return the result to the second parameter of the function "every"

---

<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:** [29 September 2020 21:04 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/16 "2020-09-29T21:04:07Z")

</div>

Yep sounds good!

---

<div class="post-metadata">

**Author:** ![michaelday](https://uzu.lurk.org/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@michaelday](https://uzu.lurk.org/u/michaelday)\
**Post date:** [27 December 2020 20:28 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/17 "2020-12-27T20:28:33Z")

</div>

Hi! I'm really confused about how you might use `every` to swap patterns.

For example, if you have this:

`d1 $ sound "bd [bd*2 sn:2] [bd ~]"`

and you want to change the pattern every 4 cycles to this

`d1 $ sound "[bd sn bd hh]"`

I guessed that you would do this

`d1 $ sound "bd [bd*2 sn:2] [bd ~]" $ every 4 $ sound "[bd sn bd hh]"`

or this

`d1 $ sound "bd [bd*2 sn:2] [bd ~]" $ every 4 ( sound "[bd sn bd hh]")`

But I guessed wrong! This feels like something you'd want to do a lot, so I'm missing something obvious? Thanks in advance for your help!

---

<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:** [27 December 2020 21:00 UTC](https://uzu.lurk.org/t/week-3-lesson-1-exploring-the-every-function-including-tackling-the-meaning-of/502/18 "2020-12-27T21:00:44Z")

</div>

Hi,

This should do it:

```haskell
d1 $ every 4 (const $ sound "[bd sn bd hh]") $ sound "bd [bd*2 sn:2] [bd ~]" 

```

The `const` function takes two inputs, and simply returns the first one. So if you give it just one input like this: `const $ sound "[bd sn bd hh]"` you end up with a new function that takes one more input, but ignores it and just returns the input it already has. That's the function that's being passed to `every 4`, so every fourth repetition, the pattern on the right is replaced with the first input to `const`. Hope that makes some sense!
