Post History
Haskell + hgl, 30 bytes ic"|"<<tv(:".")><fl<ce '.'><eq Attempt This Online! Explanation This performs a cartesian product to get all possible ways to replace characters w...
Answer
#1: Initial revision
# [Haskell](https://www.haskell.org) + [hgl](https://gitlab.com/wheatwizrad/haskell-golfing-library), 30 bytes ```haskell ic"|"<<tv(:".")><fl<ce '.'><eq ```` [Attempt This Online!](https://ato.pxeger.com/run?1=m708I7E4OzUnZ8FNxczcgvyiEoXC0sSczLTM1BSFgKLUnNKUVC6oRMDS0pI0XYubCmm2mclKNUo2NiVlGlZKekqadjZpOTbJqQrqeup2NqmFUHUOuYmZeQq2Cin5XAoKBcEKKgppCsYKSiWpxSVKCBEjBaWc_Lz01CIlFFXJiSVKEHMWLIDQAA) ### Explanation This performs a cartesian product to get all possible ways to replace characters with `.`. It then filters for those that have exactly $n$ `.`s, and concatenates them with `|`s. ## Alternative version ``` (ic"|"<ic"."<<mm tl)<<mp"?"><pST<eL<P1 ``` This version is much longer, but in my opinion it has better potential to be shorter than the above version if hgl were improved. ### Explanation This adds a dummy character `?` to the front of the list then gets all ways to partition that list, and filters the list down to those that have $n+1$ parts. Then it takes removes the first character of each partition, and concatenates them back together with `.` as the separator. Finally it concatenates all those together with `|` as the separator. # Reflection ## Reflections on the 30 byte version * `ce` and `eq` could be combined into a single function which checks if an element occurs exactly $n$ times. This could actually be made to short circuit faster than `ce` and `eq` do by default. * There should be a function that takes two elements and creates a list containing just those elements. This could be done in terms of free monoids. It would not save bytes here since `(:".")` is so short, but it is close to being useful here. ## Reflections on the alternative version * There should be a function to partition a list into chunks of a certain size. * I could really use `m4` both as a prefix or an infix here. * Here I do a two dimensional intercalation using two `ic`s. There could be a builtin that does this.