简体   繁体   English

为什么Elm使用'++'运算符来连接字符串?

[英]Why does Elm use the '++' operator to concatenate strings?

I am learning Elm and I find a lot of things that are attractive about it, such as its elegance and simplicity. 我正在学习榆树,我发现很多有吸引力的东西,比如它的优雅和简洁。 However, one aspect that I find puzzling is its use of "++" to concatenate strings. 然而,我觉得令人费解的一个方面是使用“++”来连接字符串。 For example: 例如:

> "hello" ++ " world"
"hello world"

Addition works the way you would expect it. 添加按照您期望的方式工作。

> 2 + 3 + 9
14

The majority of high level languages such as C#/Java/JavaScript/Python use a single plus "+" in concatenating strings in the analogous way multiple numbers would be summed. 大多数高级语言(如C#/ Java / JavaScript / Python)在串联字符串中使用单个加“+”,类似地将多个数字相加。 It seems so much more intuitive, as there is a certain consistency in concatenating strings like summing numbers. 它看起来更加直观,因为在连接字符串中存在一定的一致性,如求和数。

Does anyone know the logic behind the design decision to use a ++ instead of + in this context? 有没有人知道在这种情况下使用++代替+的设计决策背后的逻辑?

Elm allows you to define polymorphic functions. Elm允许您定义多态函数。

Parametric polymorphism is when a function can be applied to elements of any types: 参数多态是指函数可以应用于任何类型的元素:

f : (a, b) -> (b, a)
f (x, y) = (y, x)

Ad-hoc polymorphism is when a function can be applied to elements of some types: Ad-hoc多态性是指函数可以应用于某些类型的元素:

g : appendable -> appendable -> appendable -> appendable
g x y z = x ++ y ++ z

h : number -> number -> number
h x y = (x + 2) * y

The type variables number and appendable are special because they represent a subset of all Elm types. 类型变量numberappendable是特殊的,因为它们代表所有Elm类型的子集。 List and String are appendable types while Float and Int are number types. ListStringappendable类型,而FloatInt是数字类型。

It could theoretically be possible to instead define a hasPlus type variable which would include List , String , Float and Int , but then when defining a polymorphic function you would need to be aware that it is possible that x + y is different than y + x and that would be quite a burden if you are actually thinking about numbers... 理论上可以改为定义一个包含ListStringFloatInthasPlus类型变量,但是在定义多态函数时,你需要知道x + y可能与y + x不同。如果你真的在考虑数字,这将是一个相当大的负担......

This may be to avoid overloading an operator. 这可能是为了避免操作员过载。

It can be useful to statically infer types or even improve readability when variables are used, as it is then clear what is being done (string concatenation if ++ or arithmetics if + ). 在使用变量时静态推断类型甚至提高可读性可能很有用,因为它可以清楚地完成了什么(字符串连接,如果++或算术,如果+ )。

In languages that are not strongly typed (I do not know if this is the case in Elm), using the same notation can make it impossible to know what is being done on variables before actually running the program, which makes it slower. 在非强类型语言中(我不知道Elm中是否存在这种情况),使用相同的表示法会使得在实际运行程序之前无法知道对变量执行了什么操作,这使得速度变慢。

XQuery uses || XQuery使用|| instead of + too, some languages even use different notations for decimals, like +. 而不是+ ,有些语言甚至使用不同的小数表示法,如+. in CAML. 在CAML。

Concatenation and addition are completely different operations with different properties. 连接和添加是具有不同属性的完全不同的操作。 For example, addition is commutative (on integers, floats are different beasts), while concatenation most definitely is not. 例如,加法是可交换的(在整数上,浮点数是不同的野兽),而连接最绝对不是。 The arbitrary decision to re-use the operators by some languages is the strongest connection between them you can find. 通过某些语言重新使用运算符的任意决定是您可以找到的最强连接。

And even if the overloading made sense, you would hit the static nature of the language – what should the type of such operator be? 即使重载是有意义的,你也会遇到语言的静态性质 - 这种运算符的类型应该是什么?

Currently the operator works on magic type number : 目前,操作员使用魔术类型number

(+) : number -> number -> number

While you could have a new magic type numberorstring and the + function would be polymorphic with two different semantics, this would only be introducing even more magic into the language. 虽然你可以拥有一个新的魔术类型numberorstring ,而+函数将具有两种不同语义的多态性,但这只会在语言中引入更多魔法。

According to the docs , the ++ operator is used for appending lists: 根据文档++运算符用于附加列表:

-- alias for appending lists and two lists
append xs ys = xs ++ ys
xs = [1,2,3]
ys = [4,5,6]

-- All of the following expressions are equivalent:
a1 = append xs ys
a2 = xs ++ ys

b2 = (++) xs ys

c1 = (append xs) ys
c2 = ((++) xs) ys

Prior to version 0.9, strings were represented as a Haskell-inspired list of characters. 在0.9版之前,字符串被表示为受Haskell启发的字符列表。 Version 0.9 introduced a new string library (see the announcement here), so it looks like the ++ operator persisted despite strings no longer being represented as a list. 版本0.9引入了一个新的字符串库(参见此处的公告 ),因此尽管字符串不再表示为列表,但看起来像++运算符仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么“as”运算符在 C# 中不使用隐式转换运算符? - Why does the “as” operator not use an implicit conversion operator in C#? elm type alias 类型检查没有发生,为什么会编译? - elm type alias type checking does not happen, Why does it compile? 为什么 &amp;&amp; 运算符会产生第二个操作数的类型 - Why does the && operator produce the type of the second operand 为什么不能使用is运算符来区分bool和Nullable <bool> ? - Why is it not possible to use the is operator to discern between bool and Nullable<bool>? 为什么在三元运算符中使用“0”会返回第一个值? - Why does using “0” with the ternary operator return the first value? 为什么Visual Basic允许在算术语句中将字符串添加到Integer中? - Why does Visual Basic allow Strings to be added to Integers in arithmetic statements? elm其他地方如何使用记录的字段类型 - How do I use the field type of a record in other places in elm 为什么console.log在某些情况下会使用引号记录字符串? - Why does console.log log strings with quotes in some cases? 为什么 TypeScript 使用“Like”类型? - Why does TypeScript use “Like” types? 为什么 Kotlin 会更改 List 类型的列表<list<int> &gt; 列出<any>如果使用“+”运算符添加列表? </any></list<int> - Why does Kotlin change a list of type List<List<Int>> to List<Any> if a list is added with the "+" operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM