More on the dollar sign

Hey all!
In the docs or elsewehere I sometimes find the dollar sign used inside brackets like so:

d1 $ segment 16 $ n (scale "minor" $ floor <$> (range 0 14 sine))
  # sound "supersaw"

There the <$> works kind of like the usual $: taking the right-hand side stuff and passing it to the left-hand side function (floor). But I found some uses of <$> that I can't really wrap my head around here: tidal-workshop/workshop.tidal at master · tidalcycles/tidal-workshop · GitHub

So I wonder in general, how do these angle brackets change what $ usually does?

Other than that, I sometimes saw the dollar sign used in paratheses ($), e.g.:

d1 $ spread ($) [gap 4, striate 4] $ sound "ho ho:2 ho:3 hc"

This seems to allow spread to take an array of functions as an input and spread it across a pattern. But I wonder how to actually read that ($). Does anyone know?

1 Like

The $ is an operator in haskell with the following definition

($) :: (a -> b) -> a -> b
($) f x = f x

so seemingly it doesn't actually do anything, but the thing is that it has a very low precedence, so whatever is on it's right side will be evaluated first and than passed to whatever function is on it's left side. This is very useful for not having to type too many brackets.

if it is used without brackets, then it is in infix notation, which means you can write it between two expressions (like f $ x). If you write it in brackets, it is in prefix notation, so you can write ($) f x which doesn't make much sense unless you want to pass the operator itself as a function like in the example with spread.

Now the <$> operator has the following definition:

(<$>) :: Functor f => (a -> b) -> f a -> f b
(<$>) = fmap

this function basically defines the typeclass Functor. For us it is only important that the type constructor Pattern is part of that typeclass. More specifically we get the concrete function:

(<$>) :: (a -> b) -> Pattern a -> Pattern b

It is quite similar to ($) in that it applies a function to an input, but it also "lifts" that function to work on patterns, and not only on primitive values.

In your example (range 0 14 sine) is of type Pattern Double and you would like to apply floor to the Doubles inside the Pattern (think of patterns as containers). You can not just apply floor directly (with $), but you can use <$> instead.

I hope this makes it somewhat clear, but please feel free to ask questions! :slight_smile:

8 Likes