Skip to content

Commit

Permalink
feat: add haskell str concat ops til
Browse files Browse the repository at this point in the history
  • Loading branch information
jthodge committed Aug 12, 2024
1 parent 19d0eda commit a1fa1ff
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions haskell/string-concatenation-operators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# String Concatenation Operators

E.g. the following two functions:

## `++`

```haskell
wrapHtml content = "<html><body>" ++ content ++ "</body></html>"
```

## `<>`

```haskell
wrapHtml content = "<html><body>" <> content <> "</body></html>"
```

## Operator Comparison

### Operator

`++` is the traditional list concatenation operator in Haskell, while `<>` is the more general "mappend" operator from the `Semigroup` typeclass.

### Type flexibility

`++` works only with lists (including strings, which are lists of characters), while `<>` can work with any type that implements the `Semigroup` typeclass, including `String`, `Text`, `ByteString` (and others).

### Performance

For strings, `<>` can be more efficient, especially when working with `Text` or `ByteString` types, as it allows for optimized concatenation.

### Readability

Some developers find `<>` more readable, especially when dealing with multiple concatenations.

In practice, for simple string concatenation like the examples above, both will work identically with `String` types. However, using `<>` more extensible and improves flexibility, because the underlying string representation (e.g., to `Text`) can be changed in the future.

0 comments on commit a1fa1ff

Please sign in to comment.