Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Build a replacement ball in regex.

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...

posted 11mo ago by WheatWizard‭

Answer
#1: Initial revision by user avatar WheatWizard‭ · 2023-06-22T14:34:01Z (11 months ago)
# [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.