# What's the difference between `|+|` and `(+) <$> a <*> b`

Why is it sometimes necessary to use completely different syntax to combine patterns?

This website is no longer active. But we are waiting for your question elsewhere, find us here!

What's the difference between `|+|` and `(+) <$> a <*> b`

asked
**
2017-01-07 09:36:53 -0500
**

This post is a wiki. Anyone with karma >1 is welcome to improve it.

Why is it sometimes necessary to use completely different syntax to combine patterns?

add a comment

0

answered
**
2017-01-07 10:19:06 -0500
**

This post is a wiki. Anyone with karma >1 is welcome to improve it.

The core issue is the difference between basic Patterns and what Tidal calls "ParamPatterns".

"Pattern" is a generic class of types - you can have Pattern Int: `"0 1 2"`

, a Pattern Double: `"0.1 0.8"`

, a Pattern String: `"rave arpy"`

or any other type. *However*, note that these Patterns don't have a name in front like `n`

or `sound`

or `speed`

- they're "basic" types, and don't yet mean anything to a synthesizer (like SuperDirt).

Since these are patterns of types such as "Int" or "Double", you can combine them with ordinary math, *as long as you tell Haskell* to "lift" or "map" the operators so they work on Patterns of numbers instead of just normal numbers. So that's where you see expressions like

```
(+) <$> "0 1 2" <*> "4"
```

or

```
liftA2 (+) "0 1 2" "4"
```

(which are equivalent). The `<$>`

`<*>`

and `liftA2`

stuff isn't special to Tidal; it's how you work with certain kinds of "types of types" in Haskell (specifically Applicative Functors, but you don't have to know that).

If you want to actually make music, though, you've got to tell the computer what to *do* with these numbers, which is where all the parameter keywords like `s`

`n`

`speed`

`shape`

`cutoff`

etc. come in. Deep down, these basically pair the numbers (or strings in some cases) in the "basic" pattern with a parameter to form a ParamPattern, so when messages are sent to SuperDirt it receives a bundles of things like `{speed: 0.5}`

.

For *ParamPatterns* there are some special operators, because "adding" `speed "1 1 2"`

and `speed "3"`

is very different from "adding" `speed "1 1 2"`

and `crush "3"`

. The latter is combining completely different parameters!

So that's where the operators `|=|`

(aka `#`

), `|+|`

, `|*|`

, and `|-|`

come in. These work on ParamPatterns, and will do the math operation **only** when combining two patterns of the same parameter. So you can type

```
speed "1 1 2" |+| speed "3"
```

or even

```
speed "1 1 2" |+| crush "3"
```

which will just send both parameters ("merge") without adding the numbers.

But you **cannot** write `(+) <$> (speed "1 1 2") <*> (speed "3")`

, because Haskell only knows how to use `+`

on numbers, not on parameters.

Often you can do things either way, so whichever is most convenient. Some Tidal functions (like `striate`

) only work on ParamPatterns, so they can force you to use ParamPattern operators. But these two are completely equivalent:

```
n ((+) <$> (run 8) <*> "2")
n (run 8) |+| n "2"
```

If you want to more complex math than just arithmetic, it'll be much easier to work with the number pattern before turning it into a ParamPattern

```
speed (fmap ((**2) . cos . (/8)) run 32)
```

which makes the number pattern `run 32`

, divides by 8, takes the cosine, and squares it.

Asked: **
2017-01-07 09:36:53 -0500
**

Seen: **133 times**

Last updated: **Jan 07**

Copyright TidalCycles, 2016 Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.