# Haskell 🔄 tidal

**URL:** <https://uzu.lurk.org/t/haskell-tidal/4198>\
**Category:** TidalCycles\
**Tags:** innards\
**Created:** [5 August 2022 08:17 UTC](https://uzu.lurk.org/t/haskell-tidal/4198 "2022-08-05T08:17:48Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![zzkt](https://uzu.lurk.org/user_avatar/uzu.lurk.org/zzkt/32/2342_2.png) [@zzkt](https://uzu.lurk.org/u/zzkt)\
**Post date:** [5 August 2022 08:17 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/1 "2022-08-05T08:17:48Z")

</div>

recently i've been working with sonification of some time series data and getting into the non-live aspects of tidal and haskell. what started as adhoc experiments in tidal and supercollider has been getting a bit more structured, so thinking now about how to write decent haskell and tidal longer than a few lines at a time...

so wondering if anyone has suggestions, or ideas about the benefits/complications of using non pattern related code in .tidal or using tidal patterns in .hs?

---

<div class="post-metadata">

**Author:** ![zzkt](https://uzu.lurk.org/user_avatar/uzu.lurk.org/zzkt/32/2342_2.png) [@zzkt](https://uzu.lurk.org/u/zzkt)\
**Post date:** [5 August 2022 08:26 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/2 "2022-08-05T08:26:29Z")

</div>

perhaps as a concrete example, using data from a spreadsheet as source of patterns and sample maps...

what would be an idiomatic way of using sequences as pattern sources?

seems like there would some way of mixing or filtering based on type (e.g. \_CellText or \_CellDouble) but where or when to?

etc?

```haskell
slonkCell :: Int -> Int -> IO String
slonkCell row col = do
  bs <- L.readFile dataFile
  let value = toXlsx bs ^? ixSheet "Taul1" .
              cellValueAt (row, col) . _Just -- . _CellText -- . _CellDouble
  case value of
    Nothing -> return "Nothing"
    Just val -> return $ show val

main :: IO [(String)]
main = do
  let range2 = [(1, 1), (2, 1), (1, 3)]
      seq = mapM (\(x, y) -> slonkCell x y) range2
  seq

```

---

<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:** [5 August 2022 10:56 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/3 "2022-08-05T10:56:17Z")

</div>

Well the usual tidal commands are IO actions so you could do something like

```haskell
import Sound.Tidal.Context

main :: IO [(String)]
main = do
  tidal <- startTidal (superdirtTarget {oLatency = 0.05, oAddress = "127.0.0.1", oPort = 57120}) (defaultConfig {cVerbose = True, cFrameTimespan = 1/20})
  -- let range = [1..5]
  let range2 = [(1, 1), (2, 1), (1, 3)]
  let seq = mapM (\(x, y) -> slonkCell x y) range2
  streamReplace tidal "thingie" $ sound $ fromList seq
  -- would need to put a sleep action or something so the program doesn't terminate..

```

You'd want to run / compile with `-XOverloadedStrings` for mininotation to work.

---

<div class="post-metadata">

**Author:** ![zzkt](https://uzu.lurk.org/user_avatar/uzu.lurk.org/zzkt/32/2342_2.png) [@zzkt](https://uzu.lurk.org/u/zzkt)\
**Post date:** [5 August 2022 11:05 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/4 "2022-08-05T11:05:10Z")

</div>

Thanks. seems to make sense, still building my intuitions around haskell...

---

<div class="post-metadata">

**Author:** ![zzkt](https://uzu.lurk.org/user_avatar/uzu.lurk.org/zzkt/32/2342_2.png) [@zzkt](https://uzu.lurk.org/u/zzkt)\
**Post date:** [5 August 2022 11:15 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/5 "2022-08-05T11:15:47Z")

</div>

Perhaps a more basic question re. typing, in the example `CellValue` could have constructors `CellText` or `CellDouble` is there a way to assemble a list based on the type? or is there another approach?

---

<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:** [5 August 2022 11:51 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/6 "2022-08-05T11:51:11Z")

</div>

Which library are you using?

---

<div class="post-metadata">

**Author:** ![zzkt](https://uzu.lurk.org/user_avatar/uzu.lurk.org/zzkt/32/2342_2.png) [@zzkt](https://uzu.lurk.org/u/zzkt)\
**Post date:** [5 August 2022 12:04 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/7 "2022-08-05T12:04:04Z")

</div>

Codec.Xlsx

---

<div class="post-metadata">

**Author:** ![mvdirty](https://uzu.lurk.org/user_avatar/uzu.lurk.org/mvdirty/32/2179_2.png) [@mvdirty](https://uzu.lurk.org/u/mvdirty)\
**Post date:** [5 August 2022 14:43 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/8 "2022-08-05T14:43:01Z")

</div>

I don’t know the library in question, but from a quick glance at `CellValue` in hackage, and from your code up at top of thread, I’d imagine you’ll find use in pattern matching ala

```haskell
case value of
  Just (CellDouble foo) -> ...
  Just (CellText bar) -> ...
  _ -> ...

```

Depending on exactly what `value` is, and what you mean by “assemble a list based on type”.

It’s probably also worth taking a look at functions along the line of `catMaybes`:

- [[Maybe a] -\> [a] - Hoogle](https://hoogle.haskell.org/?hoogle=%5BMaybe%20a%5D%20-%3E%20%5Ba%5D)
- [f (Maybe a) -\> f a - Hoogle](https://hoogle.haskell.org/?hoogle=f+%28Maybe+a%29+-%3E+f+a)

---

<div class="post-metadata">

**Author:** ![zzkt](https://uzu.lurk.org/user_avatar/uzu.lurk.org/zzkt/32/2342_2.png) [@zzkt](https://uzu.lurk.org/u/zzkt)\
**Post date:** [5 August 2022 15:20 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/9 "2022-08-05T15:20:45Z")

</div>

Thanks @mvdirty the typecase style pattern matching does work, for example returning a list like `["CellText \"Text1\"","CellText \"Text2\"","CellDouble 1001.1"]` (or without the prefix from `show`) but it feels like I'm missing the point of a static type system by doing string manipulation to get typed values...

Perhaps to rephrase, for now I'm reading from a spreadsheet, which produces values like `"name"` of type `CellText` or a number of type `CellDouble` I'd like to use an intermediate representation like `["name",1,0,1,0,0]` (or `["name", 331, 403, 398 ..]`) which could be transformed to a tidal pattern like `s "name ~ name ~ ~" `, or use the tail of the list as control values, but it's not obvious to me what sort of type signatures make sense, or where it's easiest to do the required type casting.

---

<div class="post-metadata">

**Author:** ![zzkt](https://uzu.lurk.org/user_avatar/uzu.lurk.org/zzkt/32/2342_2.png) [@zzkt](https://uzu.lurk.org/u/zzkt)\
**Post date:** [5 August 2022 15:44 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/10 "2022-08-05T15:44:40Z")

</div>

thanks for the `catMaybes` tip. looks useful

---

<div class="post-metadata">

**Author:** ![mvdirty](https://uzu.lurk.org/user_avatar/uzu.lurk.org/mvdirty/32/2179_2.png) [@mvdirty](https://uzu.lurk.org/u/mvdirty)\
**Post date:** [5 August 2022 16:11 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/11 "2022-08-05T16:11:37Z")

</div>

Just to put it out there, you shouldn’t need string manipulation unless you need to parse inside the content of text cell values themselves.

(Below I have to make at least some assumptions about where you’re trying to take your code, but hopefully I’ll be able to cover some generally helpful material.)

If you step back from that list structure a bit and consider exactly what you are trying to represent conceptually, is it really a list you’re looking for? [NB: I say this because carrying arbitrary types of values in lists is somewhat antithetical to Haskell and suggests that deeper type design is needed. More on that as we go…]

Is `”name”` somehow conceptually distinct from the set of numbers which follow? Such that the list could instead be a structure containing a field where `”name”` would go and a list where the numbers would appear? Are those numbers just a list? Or is there positional value to the numbers within it, such that they could become their own fields within the structure? and so on.

Or is it really just a list, meant to carry strings and numbers interchangeably, perhaps with some semantic meaning to strings versus numbers? If so then you could create a new sum type with data constructors to distinguish the “kinds” of values. (`CellValue` itself is like this, for example, encoding various kinds of cell values into a sum type.) Your new sum type could then be carried in other structures like lists, and then pattern-matched on in order to apply different behaviours to different kinds of items.

Or perhaps both combined would provide the strongest model of what you’re going for, ala a structure carrying fields of other structures (like lists) of sum types, and so on.

Haskell leans pretty hard into encoding concepts, semantics, etc. into the type system. In fact, I dare say its use is predicated on it. With a bit of time spent defining a model of what you want, I’m sure you’ll be able to come up with a very safe and productive encoding.

---

<div class="post-metadata">

**Author:** ![zzkt](https://uzu.lurk.org/user_avatar/uzu.lurk.org/zzkt/32/2342_2.png) [@zzkt](https://uzu.lurk.org/u/zzkt)\
**Post date:** [8 August 2022 09:10 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/12 "2022-08-08T09:10:07Z")

</div>

> [@mvdirty](#):
>
> Haskell leans pretty hard into encoding concepts, semantics, etc. into the type system. In fact, I dare say its use is predicated on it. With a bit of time spent defining a model of what you want, I’m sure you’ll be able to come up with a very safe and productive encoding.

I spent some time playing around with sum types and records as suggested, and can certainly see the appeal. Some of the more useful reading...

- [Sum Types in Haskell — Monday Morning Haskell](https://mmhaskell.com/haskell-data/sum-types)
- [Stack Builders - CSV encoding and decoding in Haskell with Cassava](https://www.stackbuilders.com/blog/csv-encoding-decoding/)
- [Haskell - Record Syntax](https://devtut.github.io/haskell/record-syntax.html)

---

<div class="post-metadata">

**Author:** ![zzkt](https://uzu.lurk.org/user_avatar/uzu.lurk.org/zzkt/32/2342_2.png) [@zzkt](https://uzu.lurk.org/u/zzkt)\
**Post date:** [8 August 2022 09:19 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/13 "2022-08-08T09:19:01Z")

</div>

Since most of my exploratory programming tends to rely on the lisp family (for better or worse) "carrying arbitrary types of values in lists" is usually where it begins. I'll probably return to type system wrangling at some point, but for now, it's back to dynamic typing and strings of patterns...

```haskell
(define (row->pattern name)
  (string-append "d1 $ s \""
                 (let ((seq (get-row-by-name name)))
                   (replace seq 1 name))
                 "\"" ))

```

---

<div class="post-metadata">

**Author:** ![zzkt](https://uzu.lurk.org/user_avatar/uzu.lurk.org/zzkt/32/2342_2.png) [@zzkt](https://uzu.lurk.org/u/zzkt)\
**Post date:** [9 August 2022 14:09 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/14 "2022-08-09T14:09:49Z")

</div>

Maybe the "[Write Yourself a Scheme in 48 Hours](https://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours)" book is another possible entry point...

---

<div class="post-metadata">

**Author:** ![mvdirty](https://uzu.lurk.org/user_avatar/uzu.lurk.org/mvdirty/32/2179_2.png) [@mvdirty](https://uzu.lurk.org/u/mvdirty)\
**Post date:** [9 August 2022 15:21 UTC](https://uzu.lurk.org/t/haskell-tidal/4198/15 "2022-08-09T15:21:46Z")

</div>

The last time I used Lisp was nearly thirty years ago, but I definitely get ya. 👍 Even spent some time on a Lisp machine. Parens all the way down. 😉

With that background in mind, lists of a sum type with simple data constructors would be a good next step when the time comes. It’ll add a pile of safe and easy pattern-matching power without dragging in too many concepts/constructs at once.
