# Difference between revisions of "Understanding the $"

(Marked this version for translation) |
|||

Line 1: | Line 1: | ||

<translate> | <translate> | ||

+ | <!--T:1--> | ||

The dollar (<syntaxhighlight lang="Haskell" inline>$</syntaxhighlight>) is a mysterious thing. It doesn't really do anything, but is super useful. It's easy to get it mixed up with other operators in Tidal, for example <syntaxhighlight lang="Haskell" inline>#</syntaxhighlight>, because in a way they both 'join things together'. But what is <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight>, exactly? | The dollar (<syntaxhighlight lang="Haskell" inline>$</syntaxhighlight>) is a mysterious thing. It doesn't really do anything, but is super useful. It's easy to get it mixed up with other operators in Tidal, for example <syntaxhighlight lang="Haskell" inline>#</syntaxhighlight>, because in a way they both 'join things together'. But what is <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight>, exactly? | ||

+ | <!--T:2--> | ||

The <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> is used a ''lot'' in Haskell (the language which Tidal lives inside). Like a lot of things in Haskell, <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> is a ''function''. Like all operators (e.g. <syntaxhighlight lang="Haskell" inline>+</syntaxhighlight>), it has two inputs - the left side, and the right side, and has one output. The left input must be a function, and all that <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> does is pass what's on the right hand side, and give it to that function. | The <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> is used a ''lot'' in Haskell (the language which Tidal lives inside). Like a lot of things in Haskell, <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> is a ''function''. Like all operators (e.g. <syntaxhighlight lang="Haskell" inline>+</syntaxhighlight>), it has two inputs - the left side, and the right side, and has one output. The left input must be a function, and all that <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> does is pass what's on the right hand side, and give it to that function. | ||

+ | <!--T:3--> | ||

In other words, in this expression: | In other words, in this expression: | ||

+ | <!--T:4--> | ||

<syntaxhighlight lang="Haskell"> | <syntaxhighlight lang="Haskell"> | ||

rev $ "1 2 3" | rev $ "1 2 3" | ||

</syntaxhighlight> | </syntaxhighlight> | ||

+ | <!--T:5--> | ||

... the dollar takes <syntaxhighlight lang="Haskell" inline>"1 2 3"</syntaxhighlight> and passes it to the function <syntaxhighlight lang="Haskell" inline>rev</syntaxhighlight>. So actually the above is the same as this: | ... the dollar takes <syntaxhighlight lang="Haskell" inline>"1 2 3"</syntaxhighlight> and passes it to the function <syntaxhighlight lang="Haskell" inline>rev</syntaxhighlight>. So actually the above is the same as this: | ||

+ | <!--T:6--> | ||

<syntaxhighlight lang="Haskell"> | <syntaxhighlight lang="Haskell"> | ||

rev "1 2 3" | rev "1 2 3" | ||

</syntaxhighlight> | </syntaxhighlight> | ||

+ | <!--T:7--> | ||

So if we can do without it, why is it useful? Lets look at a slightly more complex example: | So if we can do without it, why is it useful? Lets look at a slightly more complex example: | ||

+ | <!--T:8--> | ||

<syntaxhighlight lang="Haskell"> | <syntaxhighlight lang="Haskell"> | ||

fast 2 $ rev "1 2 3" | fast 2 $ rev "1 2 3" | ||

</syntaxhighlight> | </syntaxhighlight> | ||

+ | <!--T:9--> | ||

Here the dollar takes care of passing <syntaxhighlight lang="Haskell" inline>rev "1 2 3"</syntaxhighlight> to <syntaxhighlight lang="Haskell" inline>fast 2</syntaxhighlight>. If we missed it out, then we'd get an error. | Here the dollar takes care of passing <syntaxhighlight lang="Haskell" inline>rev "1 2 3"</syntaxhighlight> to <syntaxhighlight lang="Haskell" inline>fast 2</syntaxhighlight>. If we missed it out, then we'd get an error. | ||

+ | <!--T:10--> | ||

<syntaxhighlight lang="Haskell"> | <syntaxhighlight lang="Haskell"> | ||

-- this gives an error! | -- this gives an error! | ||

Line 29: | Line 39: | ||

</syntaxhighlight> | </syntaxhighlight> | ||

+ | <!--T:11--> | ||

That's because the computer will first read <syntaxhighlight lang="Haskell" inline>fast 2</syntaxhighlight>, then <syntaxhighlight lang="Haskell" inline>rev</syntaxhighlight>, and try to treat <syntaxhighlight lang="Haskell" inline>rev</syntaxhighlight> as a pattern to be speeded up. But on its own, <syntaxhighlight lang="Haskell" inline>rev</syntaxhighlight> isn't a pattern, but a function for transforming pattern. | That's because the computer will first read <syntaxhighlight lang="Haskell" inline>fast 2</syntaxhighlight>, then <syntaxhighlight lang="Haskell" inline>rev</syntaxhighlight>, and try to treat <syntaxhighlight lang="Haskell" inline>rev</syntaxhighlight> as a pattern to be speeded up. But on its own, <syntaxhighlight lang="Haskell" inline>rev</syntaxhighlight> isn't a pattern, but a function for transforming pattern. | ||

+ | <!--T:12--> | ||

To avoid this error, we could use parenthesis: | To avoid this error, we could use parenthesis: | ||

+ | <!--T:13--> | ||

<syntaxhighlight lang="Haskell"> | <syntaxhighlight lang="Haskell"> | ||

fast 2 (rev "1 2 3") | fast 2 (rev "1 2 3") | ||

</syntaxhighlight> | </syntaxhighlight> | ||

+ | <!--T:14--> | ||

Here the brackets make sure <syntaxhighlight lang="Haskell" inline>rev "1 2 3"</syntaxhighlight> is calculated first, before it is passed as a pattern to <syntaxhighlight lang="Haskell" inline>fast 2</syntaxhighlight>. | Here the brackets make sure <syntaxhighlight lang="Haskell" inline>rev "1 2 3"</syntaxhighlight> is calculated first, before it is passed as a pattern to <syntaxhighlight lang="Haskell" inline>fast 2</syntaxhighlight>. | ||

+ | <!--T:15--> | ||

So, both <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> and parenthesis can be used to control which code is calculated first. The <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> is often used to avoid having to match opening and closing brackets, but sometimes parenthesis makes more sense. | So, both <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> and parenthesis can be used to control which code is calculated first. The <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> is often used to avoid having to match opening and closing brackets, but sometimes parenthesis makes more sense. | ||

+ | <!--T:16--> | ||

Note that you ''can't'' use <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> with operators. For example: | Note that you ''can't'' use <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> with operators. For example: | ||

+ | <!--T:17--> | ||

<syntaxhighlight lang="Haskell"> | <syntaxhighlight lang="Haskell"> | ||

-- this doesn't work either! | -- this doesn't work either! | ||

Line 51: | Line 68: | ||

− | = Comparing <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> and <syntaxhighlight lang="Haskell" inline>#</syntaxhighlight> = | + | = Comparing <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> and <syntaxhighlight lang="Haskell" inline>#</syntaxhighlight> = <!--T:18--> |

+ | <!--T:19--> | ||

So, <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> is used to join a parameter (on the right) with a function (on the left). <syntaxhighlight lang="Haskell" inline>#</syntaxhighlight> (and all its friends <syntaxhighlight lang="Haskell" inline>|+|</syntaxhighlight>, <syntaxhighlight lang="Haskell" inline>|*|</syntaxhighlight>, etc) are used to combine a pattern on the right with a pattern on the left. | So, <syntaxhighlight lang="Haskell" inline>$</syntaxhighlight> is used to join a parameter (on the right) with a function (on the left). <syntaxhighlight lang="Haskell" inline>#</syntaxhighlight> (and all its friends <syntaxhighlight lang="Haskell" inline>|+|</syntaxhighlight>, <syntaxhighlight lang="Haskell" inline>|*|</syntaxhighlight>, etc) are used to combine a pattern on the right with a pattern on the left. | ||

[[Category:Reference|$]] | [[Category:Reference|$]] | ||

</translate> | </translate> |

## Latest revision as of 09:31, 16 November 2019

The dollar (`$`

) is a mysterious thing. It doesn't really do anything, but is super useful. It's easy to get it mixed up with other operators in Tidal, for example `#`

, because in a way they both 'join things together'. But what is `$`

, exactly?

The `$`

is used a *lot* in Haskell (the language which Tidal lives inside). Like a lot of things in Haskell, `$`

is a *function*. Like all operators (e.g. `+`

), it has two inputs - the left side, and the right side, and has one output. The left input must be a function, and all that `$`

does is pass what's on the right hand side, and give it to that function.

In other words, in this expression:

```
rev $ "1 2 3"
```

... the dollar takes `"1 2 3"`

and passes it to the function `rev`

. So actually the above is the same as this:

```
rev "1 2 3"
```

So if we can do without it, why is it useful? Lets look at a slightly more complex example:

```
fast 2 $ rev "1 2 3"
```

Here the dollar takes care of passing `rev "1 2 3"`

to `fast 2`

. If we missed it out, then we'd get an error.

```
-- this gives an error!
fast 2 rev "1 2 3"
```

That's because the computer will first read `fast 2`

, then `rev`

, and try to treat `rev`

as a pattern to be speeded up. But on its own, `rev`

isn't a pattern, but a function for transforming pattern.

To avoid this error, we could use parenthesis:

```
fast 2 (rev "1 2 3")
```

Here the brackets make sure `rev "1 2 3"`

is calculated first, before it is passed as a pattern to `fast 2`

.

So, both `$`

and parenthesis can be used to control which code is calculated first. The `$`

is often used to avoid having to match opening and closing brackets, but sometimes parenthesis makes more sense.

Note that you *can't* use `$`

with operators. For example:

```
-- this doesn't work either!
4 * $ 2 + 3
-- but this does
4 * (2 + 3)
```

# Comparing `$`

and `#`

So, `$`

is used to join a parameter (on the right) with a function (on the left). `#`

(and all its friends `|+|`

, `|*|`

, etc) are used to combine a pattern on the right with a pattern on the left.