# Whenmod 8.5 6.5 possible?

**URL:** <https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722>\
**Category:** Uncategorized\
**Created:** [9 January 2021 15:57 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722 "2021-01-09T15:57:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pyrrhic](https://uzu.lurk.org/user_avatar/uzu.lurk.org/pyrrhic/32/1563_2.png) [@pyrrhic](https://uzu.lurk.org/u/pyrrhic)\
**Post date:** [9 January 2021 15:57 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722/1 "2021-01-09T15:57:23Z")

</div>

I'm hoping to find a way to input partial cycles to control when whenmod takes effect.

Is there a way to do this? I'm hoping I'm just missing the right syntax.

Thank you

---

<div class="post-metadata">

**Author:** ![th4](https://uzu.lurk.org/user_avatar/uzu.lurk.org/th4/32/505_2.png) [@th4](https://uzu.lurk.org/u/th4)\
**Post date:** [9 January 2021 16:28 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722/2 "2021-01-09T16:28:06Z")

</div>

Looking into the Tidal codebase, I see that `whenmod` is implemented as follows:

```haskell
whenmod :: Int -> Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
whenmod a b = Sound.Tidal.Core.when (\t -> (t `mod` a) >= b )

```

You can see that it uses the `mod` function to determine when to trigger the function. If, as I believe it is, it's the basic Haskell function from the `Prelude`, its type signature is `mod :: Integral a => a -> a -> a`, which means that, unfortunately, it only takes integers (or numbers behaving as such) as arguments.

(I'm realizing at that point that I could have just checked the time signature of `whenmod`, which is a very explicit `whenmod :: Int -> Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a`, but what I wrote certainly has pedagogical merits.)

What you could do is redefine `whenmod` so that it doesn't use `mod` anymore, but it's extended version `mod'` (which you will probably have to import).  
You Tidal script would then become:

```haskell
import Data.Fixed(mod')

whenmod :: Int -> Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
whenmod a b = Sound.Tidal.Core.when (\t -> (t `mod'` a) >= b )

-- your music here

```

---

<div class="post-metadata">

**Author:** ![pyrrhic](https://uzu.lurk.org/user_avatar/uzu.lurk.org/pyrrhic/32/1563_2.png) [@pyrrhic](https://uzu.lurk.org/u/pyrrhic)\
**Post date:** [9 January 2021 16:29 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722/3 "2021-01-09T16:29:40Z")

</div>

Oh wow! Will try it out asap, thank you!

---

<div class="post-metadata">

**Author:** ![pyrrhic](https://uzu.lurk.org/user_avatar/uzu.lurk.org/pyrrhic/32/1563_2.png) [@pyrrhic](https://uzu.lurk.org/u/pyrrhic)\
**Post date:** [9 January 2021 17:57 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722/4 "2021-01-09T17:57:32Z")

</div>

Pardon me for being such an annoying n00b but I evaluated the above lines in Atom, first line as single line eval and the two below together and Atom was giving no errors.

But if I try to use a whenmod 4.5 3.5 in tidal after the above it gives me this error:

```
t> 
    • No instance for (Fractional Int) arising from the literal ‘4.5’
    • In the first argument of ‘whenmod’, namely ‘4.5’
      In the expression:
        whenmod 4.5 3.5 (const $ n "[~ -12 ~]*6" # s "midi")
      In the second argument of ‘($)’, namely
        ‘whenmod 4.5 3.5 (const $ n "[~ -12 ~]*6" # s "midi") $ s "hh*16"’

```

Apparently I'm not doing it right.

---

<div class="post-metadata">

**Author:** ![th4](https://uzu.lurk.org/user_avatar/uzu.lurk.org/th4/32/505_2.png) [@th4](https://uzu.lurk.org/u/th4)\
**Post date:** [9 January 2021 20:06 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722/5 "2021-01-09T20:06:48Z")

</div>

Ah, I see what I did wrong, the type signature of `whenmod` still asks for `Int`s. This is what you get when you post code without testing it.  
I'll get back to you when I find a fix.

---

<div class="post-metadata">

**Author:** ![pyrrhic](https://uzu.lurk.org/user_avatar/uzu.lurk.org/pyrrhic/32/1563_2.png) [@pyrrhic](https://uzu.lurk.org/u/pyrrhic)\
**Post date:** [9 January 2021 20:07 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722/6 "2021-01-09T20:07:12Z")

</div>

Superb, thank you!

---

<div class="post-metadata">

**Author:** ![th4](https://uzu.lurk.org/user_avatar/uzu.lurk.org/th4/32/505_2.png) [@th4](https://uzu.lurk.org/u/th4)\
**Post date:** [9 January 2021 21:34 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722/7 "2021-01-09T21:34:41Z")

</div>

Ok, I think I've got it, the problem was that the conditional function fed to `Sound.Tidal.Core.when` takes an `Int` as an argument, which has to be explicitly converted into something that `mod'` can take as an argument. Also, it was missing some type constraints.  
Here is a version that should work (this time I have tested it):

```haskell
whenmod :: (Ord b, Real b, Num b) => b -> b -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
whenmod a b = Sound.Tidal.Core.when (\t -> (((fromIntegral t) `mod'` a) >= b ))

```

---

<div class="post-metadata">

**Author:** ![pyrrhic](https://uzu.lurk.org/user_avatar/uzu.lurk.org/pyrrhic/32/1563_2.png) [@pyrrhic](https://uzu.lurk.org/u/pyrrhic)\
**Post date:** [9 January 2021 22:29 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722/8 "2021-01-09T22:29:17Z")

</div>

This is so sweeeeeeet!! Thank you so very much for your knowledge and help with the higher level Haskell, much appreciated!

---

<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:** [10 January 2021 02:12 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722/9 "2021-01-10T02:12:49Z")

</div>

Great, a pull request would be welcome!

---

<div class="post-metadata">

**Author:** ![th4](https://uzu.lurk.org/user_avatar/uzu.lurk.org/th4/32/505_2.png) [@th4](https://uzu.lurk.org/u/th4)\
**Post date:** [14 January 2021 20:14 UTC](https://uzu.lurk.org/t/whenmod-8-5-6-5-possible/2722/10 "2021-01-14T20:14:45Z")

</div>

Done! Sorry for the delay, been busy recently.
