0

I am new to dhall with no Haskell background. How do I convert a list of string to a single string with values separated by comma? E.g.

["a", "b", "c"] -> "a,b,c"

I tried List/fold, but couldn't figure out an idiomatic way to get rid of the extra comma.

Thanks

2 Answers2

1

The Prelude has a Text/concatSep function, which is what you are looking for:

let Text/concatSep = https://prelude.dhall-lang.org/Text/concatSep

in  Text/concatSep "," [ "a", "b", "c" ]

Here is the source code, in case you are interested in how it is implemented:

Gabriella Gonzalez
  • 34,863
  • 3
  • 77
  • 135
  • Thank you, I should have looked harder beyond List methods. Yes, reading source code does help with learning dhall. – Paresh Adhia Aug 29 '19 at 23:49
  • Also, would you please explain how to interpret `let Text/concatSep = ...`? It seems `let` in **dhall** allows a special bind variable naming `Type/function`. In your solution above, any reason to not just use `let concatSep = ...` or `let TextConcatSep = ...`? – Paresh Adhia Sep 03 '19 at 02:16
  • @PareshAdhia: I could have also used `concatSep` or `Text/concatSep` like you suggested. I also could have done `let Prelude = https://prelude.dhall-lang.org/package.dhall in Prelude.Text.concatSep ...`. The only reason I named it `Text/concatSep` is purely because of a personal naming convention of mine. The reason I do it is because some utilities in the Prelude share the same suffix (e.g. `List/map` vs. `Optional/map` or `JSON/Type` vs `Map/Type`), so I use the prefix to disambiguate them – Gabriella Gonzalez Sep 04 '19 at 19:38
0

Unless someone offers a better answer, following seems to work:

\(xs: List Text) ->
    let b = {index: Natural, value: Text}
    let ys = List/indexed Text xs
    let dlm = \(i: Natural) -> if Natural/isZero i then "" else ","
in List/fold b ys Text (\(x: b) -> \(y: Text) -> "${dlm x.index}${x.value}${y}") ""