-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add haskell str concat ops til
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |