Why did Haskell decide not to use `+` to concatenate strings (list)?

Since I had trouble finding this question, I thought I'd post it here.

I'm just interested in the logic behind it, or let it be the creators' preference instead ++. I mean, using typeclass for strings that combine two strings (or rather lists) with help +doesn't seem too complicated to represent.

Edit: I have to add that in Haskell I have to suspect the reasons for this, because +and ++are functions defined in classes, whereas in java, using +string concatenation is only part of the language syntax and therefore obeys only the preferences / opinions of the authors. (Answers still say that I was right in my suspicions.)

Haskell also comes from a mathematical background and is highly dependent on mathematical syntax, so there may be deeper reasons than just preference / opinion.

+4
source share
2 answers

In short, this will cause type problems.

(+)is part of the class Num:

class  Num a  where
    (+), (-), (*)       :: a -> a -> a
    negate              :: a -> a
    abs                 :: a -> a
    signum              :: a -> a
    fromInteger         :: Integer -> a

    x - y               = x + negate y
    negate x            = 0 - x

And (++) :: [a] -> [a] -> [a].

: , (+) , (*), negate, abs, signum fromInteger . .

(+) typeclass , Plussable (+), , , 1 + 2*(2-1) Num a => a, (Plussable a, Timesable a, Minusable a) => a .. . .

+4

typeclass ,

, +, <>:

Prelude> :m +Data.Monoid
Prelude Data.Monoid> "foo" <> "bar"
"foobar"

++ , <> , Monoid.

, + Num. Monoid +, ? ; - :

Prelude Data.Monoid> Sum 2 <> Sum 3
Sum {getSum = 5}
Prelude Data.Monoid> Product 2 <> Product 3
Product {getProduct = 6}

- <>, "" , .

+14

Source: https://habr.com/ru/post/1696170/


All Articles