# Week 8, Lesson 3 - fitting values to patterns with 'fit'

**URL:** <https://uzu.lurk.org/t/week-8-lesson-3-fitting-values-to-patterns-with-fit/2236>\
**Category:** Week 8 - loads of lovely functions\
**Created:** [2 October 2020 10:58 UTC](https://uzu.lurk.org/t/week-8-lesson-3-fitting-values-to-patterns-with-fit/2236 "2020-10-02T10:58:03Z")\
**Posts on this page:** 6\
**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:** [2 October 2020 10:58 UTC](https://uzu.lurk.org/t/week-8-lesson-3-fitting-values-to-patterns-with-fit/2236/1 "2020-10-02T10:58:04Z")

</div>

Ok after some delay, I'm back to finish off week 8 finally!

Here's a look at the `fit` function. I'd be happy to see your thoughts and questions about this one!

[![](https://img.youtube.com/vi/2YlI02lqPWc/maxresdefault.jpg "Tidal club week 8 lesson 3 - fitting values to into rhythms with 'fit'") ](https://www.youtube.com/watch?v=2YlI02lqPWc)

Here's the worksheet:

```haskell
-- Lets fit things from a list, into a pattern!

-- Here's the 'type signature', what's it telling us?
fit :: Int -> [a] -> Pattern Int -> Pattern a

-- 'fit' takes a whole number, a list of things, a pattern of whole numbers,
-- and then gives back a pattern of things.

-- Int - a 'step size' - how far to advance through the list each cycle
-- [a] - a list - the things you want to put in the tattern
-- Pattern Int - a pattern of numbers referring to things in the list
-- Pattern a - the result! 'Pattern a' means it can work with any kind of
-- pattern

-- Let's start simple, with a step size of 0

d1 $ n (fit 0 [9,10,11,12,13,14] "0 1 2 3") # s "alphabet"

-- That's just cycling through four letters of the alphabet (j,k,l,m).
-- We have six numbers in our list, but we're only using the first four
-- (from 0 to 3).

-- Let's use all six, and add a bit more structure:
d1 $ n (fit 0 [9,10,11,12,13,14] "[0 3] [1 2] 4 [~ 5]") # s "alphabet"

-- Note that if you go past the end of the list, you go back to the start again.
-- So '0' and '6' end up pointing at the first of the six numbers, which is '9'
-- (which gives us 'j')
d1 $ n (fit 0 [9,10,11,12,13,14] "0 6") # s "alphabet"

-- Ok what if we start playing with that 'step size'?
d1 $ n (fit 1 [9,10,11,12,13,14] "0 1 2 ~") # s "alphabet"

-- It starts getting confusing, but you should be able to hear that each cycle,
-- the pattern moves through the list by one step, until it gets back to the
-- start again. So if it starts from 'j', 'k', 'l', the next cycle it'll shift
-- along by one and give 'k', 'l', 'm', and so on, until it starts wrapping
-- around to the start again.

-- This can be nice for generating melodies. The rhythm stays the same, but
-- the notes evolve, moving through the pattern
d1 $ note (fit 2 [0,2,7,5,12] "0 ~ 1 [2 3]") # sound "supermandolin"
  # legato 2 # gain 1.3

d2 $ n "0 ~ 2 [3*2 4*2]" # sound "cpu" # speed 2

```

---

<div class="post-metadata">

**Author:** ![earthlydelight](https://uzu.lurk.org/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@earthlydelight](https://uzu.lurk.org/u/earthlydelight)\
**Post date:** [2 October 2020 13:55 UTC](https://uzu.lurk.org/t/week-8-lesson-3-fitting-values-to-patterns-with-fit/2236/2 "2020-10-02T13:55:32Z")

</div>

Thank you, really interesting!

Maybe a tangential question: say I want to generate an array of n random integers within a range, how do I do it in haskell?

---

<div class="post-metadata">

**Author:** ![abertier64](https://uzu.lurk.org/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@abertier64](https://uzu.lurk.org/u/abertier64)\
**Post date:** [4 October 2020 18:32 UTC](https://uzu.lurk.org/t/week-8-lesson-3-fitting-values-to-patterns-with-fit/2236/3 "2020-10-04T18:32:34Z")

</div>

In Haskell you can do

```haskell
import System.Random
let pureGen = mkStdGen 137

take 5 $ randomRs(0,1.0) $ pureGen
take 100 $ randomRs(2,4) $ pureGen

```

unfortunately it is not that simple in Tidal Cycles since for obscure reasons it does not easily accept to `import System.Random`

I finally suceeded by adding `:set -package Random` at the top of my `BootTidal.hs` as explained in [Week 4 lesson 2 - random marathon: rand, irand, mininotation randomness, scramble, shuffle, choose + more - #36 by abertier64](https://club.tidalcycles.org/t/week-4-lesson-2-random-marathon-rand-irand-mininotation-randomness-scramble-shuffle-choose-more/685/36)

---

<div class="post-metadata">

**Author:** ![earthlydelight](https://uzu.lurk.org/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@earthlydelight](https://uzu.lurk.org/u/earthlydelight)\
**Post date:** [6 October 2020 08:25 UTC](https://uzu.lurk.org/t/week-8-lesson-3-fitting-values-to-patterns-with-fit/2236/4 "2020-10-06T08:25:45Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![specht](https://uzu.lurk.org/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@specht](https://uzu.lurk.org/u/specht)\
**Post date:** [27 October 2020 11:21 UTC](https://uzu.lurk.org/t/week-8-lesson-3-fitting-values-to-patterns-with-fit/2236/5 "2020-10-27T11:21:48Z")

</div>

Great function!

---

<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:** [18 December 2020 22:27 UTC](https://uzu.lurk.org/t/week-8-lesson-3-fitting-values-to-patterns-with-fit/2236/6 "2020-12-18T22:27:30Z")

</div>

Great little function and explanation!

How would one go about making it select sample banks as mentioned in the video?
