是否有强类型编程语言允许您定义新的运算符?

时间:2022-07-31 16:05:41

I am currently looking for a programming language to write a math class in. I know that there are lots and lots of them everywhere around, but since I'm going to start studying math next semester, I thought this might be a good way to get a deeper insight in to what I've learned.

我目前正在寻找一种编程语言来编写数学课。我知道周围有很多很多,但是因为我下学期要开始学习数学,所以我认为这可能是一个很好的方法。深入了解我所学到的知识。

Thanks for your replys.

谢谢你的回复。

BTW: If you are wondering what I wanted to ask:

顺便说一句:如果你想知道我想问的是什么:

"Is there a strongly typed programming language which allows you to define new operators?"

“是否有强类型编程语言允许您定义新的运算符?”

13 个解决方案

#1


Like EFraim said, Haskell makes this pretty easy:

就像EFraim所说,Haskell使这很容易:

% ghci
ghci> let a *-* b = (a*a) - (b*b)
ghci> :type (*-*)
(*-*) :: (Num a) => a -> a -> a
ghci> 4 *-* 3
7
ghci> 1.2 *-* 0.9
0.6299999999999999
ghci> (*-*) 5 3
16
ghci> :{
          let gcd a b | a > b     = gcd (a - b) b 
                      | b > a     = gcd a (b - a) 
                      | otherwise = a
      :}
ghci> :type gcd
gcd :: (Ord a, Num a) => a -> a -> a
ghci> gcd 3 6
3
ghci> gcd 12 11
1
ghci> 18 `gcd` 12
6

You can define new infix operators (symbols only) using an infix syntax. You can then use them as infix operators, or enclose them in parens to use them as a normal function.

您可以使用中缀语法定义新的中缀运算符(仅符号)。然后,您可以将它们用作中缀运算符,或将它们包含在parens中以将它们用作普通函数。

You can also use normal functions (letters, numbers, underscores and single-quotes) as operators by enclosing them in backticks.

您还可以将常规函数(字母,数字,下划线和单引号)用作运算符,方法是将它们括在反引号中。

#2


Well, you can redefine a fixed set of operators in many languages, like C++ or C#. Others, like F# or Scala allow you to define even new operators (even as infix ones) which might be even nicer for math-y stuff.

好吧,你可以用许多语言重新定义一组固定的运算符,比如C ++或C#。其他的,比如F#或Scala,你甚至可以定义新的运算符(即使是中缀运算符),这些运算符甚至可能更好用于数学运算。

#3


Maybe Haskell? Allows you to define arbitrary infix operators.

也许哈斯克尔?允许您定义任意中缀运算符。

#4


Ted Neward wrote a series of article on Scala aimed at Java developers, and he finished it off by demonstrating how to write a mathematical domain language in Scala (which, incidentally, is a statically-typed language)

Ted Neward写了一系列关于Scala的文章,针对Java开发人员,他通过演示如何在Scala中编写数学域语言(顺便提一下,这是一种静态类型的语言)来完成它。

Part 1

Part 2

Part 3

#5


In C++ you can define operators that work on other classes, but I don't think other primitive types like ints since they can't have instance methods. You could either make your own number class in C++ and redefine ALL the operators, including + * etc.

在C ++中,您可以定义适用于其他类的运算符,但我不认为其他原始类型如int,因为它们不能有实例方法。您可以在C ++中创建自己的数字类并重新定义所有运算符,包括+ *等。

To make new operators on primitive types you have to turn to functional programming (it seems from the other answers). This is fine, just keep in mind that functional programming is very different from OOP. But it will be a great new challenge and functional programming is great for math as it comes from lambda calc. Learning functional programming will teach you different skills and help you greatly with math and programming in general. :D

要在原始类型上创建新的运算符,您必须转向函数式编程(从其他答案看来)。这很好,请记住,函数式编程与OOP非常不同。但这将是一个伟大的新挑战,函数式编程对于数学非常有用,因为它来自lambda calc。学习函数式编程将教会您不同的技能,并通常帮助您进行数学和编程。 :d

good luck!

#6


Eiffel allows you to define new operators.

Eiffel允许您定义新的运算符。

http://dev.eiffel.com

#7


Inasmuch as the procedure you apply to the arguments in a Lisp combination is called an “operator,” then yeah, you can define new operators till the cows come home.

因为你应用于Lisp组合中的参数的过程被称为“操作符”,所以是的,你可以定义新的操作符直到奶牛回家。

#8


Ada has support for overriding infix operators: here is the reference manual chapter.

Ada支持覆盖中缀运算符:这是参考手册章节。

Unfortunately you can't create your own new operators, it seems you can only override the existing ones.

遗憾的是,您无法创建自己的新运算符,您似乎只能覆盖现有运算符。

type wobble is new integer range 23..89;

function "+" (A, B: wobble) return wobble is
begin
   ...
end "+";

Ada is not a hugely popular language, it has to be said, but as far as strong typing goes, you can't get much stronger.

不得不说Ada不是一种非常流行的语言,但就强打字而言,你不可能变得更强大。

EDIT:

Another language which hasn't been mentioned yet is D. It also is a strongly typed language, and supports operator overloading. Again, it doesn't support user-defined infix operators.

另一种尚未提及的语言是D.它也是一种强类型语言,并支持运算符重载。同样,它不支持用户定义的中缀运算符。

From http://www.digitalmars.com/d/1.0/rationale.html

Why not allow user definable operators?

为什么不允许用户定义运营商?

These can be very useful for attaching new infix operations to various unicode symbols. The trouble is that in D, the tokens are supposed to be completely independent of the semantic analysis. User definable operators would break that.

这些对于将新的中缀操作附加到各种unicode符号非常有用。麻烦的是,在D中,令牌应该完全独立于语义分析。用户可定义的运营商会打破这种局面。

#9


Both ocaml and f# have infix operators. They have a special set of characters that are allowed within their syntax, but both can be used to manipulate other symbols to use any function infix (see the ocaml discussion).

ocaml和f#都有中缀运算符。它们在语法中有一组特殊的字符,但两者都可用于操纵其他符号以使用任何函数中缀(参见ocaml讨论)。

#10


I think you should probably think deeply about why you want to use this feature. It seems to me that there are much more important considerations when choosing a language.

我想您应该深入思考为什么要使用此功能。在我看来,在选择语言时有更重要的考虑因素。

I can only think of one possible meaning for the word "operator" in this context, which is just syntactic sugar for a function call, e.g. foo + bar would be translated as a call to a function +(a, b).

在这种情况下,我只能想到“运算符”这个词的一个可能含义,它只是函数调用的语法糖,例如, foo + bar将被翻译为对函数+(a,b)的调用。

This is sometimes useful, but not often. I can think of very few instances where I have overloaded/defined an operator.

这有时很有用,但不常见。我可以想到很少有我重载/定义运算符的实例。

As noted in the other answers, Haskell does allow you to define new infix operators. However, a purely functional language with lazy evaluation can be a bit of a mouthful. I would probably recommend SML over Haskell, if you feel like trying on a functional language for the first time. The type system is a bit simpler, you can use side-effects and it is not lazy.

如其他答案中所述,Haskell允许您定义新的中缀运算符。然而,具有懒惰评估的纯函数语言可能有点拗口。如果您想第一次尝试使用函数式语言,我可能会推荐Sask而不是Haskell。类型系统有点简单,你可以使用副作用,它不是懒惰。

F# is also very interesting and also features units of measure, which AFAIK is unique to that language. If you have a need for the feature it can be invaluable.

F#也非常有趣,并且还具有测量单位,AFAIK是该语言独有的。如果您需要该功能,它可能是非常宝贵的。

Off the top of my head I can't think of any statically typed imperative languages with infix operators, but you might want to use a functional language for math programming anyway, since it is much easier to prove facts about a functional program.

在我的脑海中,我无法想到使用中缀运算符的任何静态类型的命令式语言,但你可能想要使用函数式语言进行数学编程,因为它更容易证明关于函数式程序的事实。

You might also want to create a small DSL if syntax issues like infix operators are so important to you. Then you can write the program in whatever language you want and still specify the math in a convenient way.

如果像中缀运算符这样的语法问题对您来说非常重要,您可能还想创建一个小型DSL。然后,您可以用您想要的任何语言编写程序,并以方便的方式指定数学。

#11


What do you mean by strong typing? Do you mean static typing (where everything has a type that is known at compile time, and conversions are restricted) or strong typing (everything has a type known at run time, and conversions are restricted)?

强类型是什么意思?你的意思是静态类型(其中一切都有编译时已知的类型,转换受到限制)或强类型(一切都在运行时已知类型,转换受到限制)?

I'd go with Common Lisp. It doesn't actually have operators (for example, adding a and b is (+ a b)), but rather functions, which can be defined freely. It has strong typing in that every object has a definite type, even if it can't be known at compile time, and conversions are restricted. It's a truly great language for exploratory programming, and it sounds like that's what you'll be doing.

我会选择Common Lisp。它实际上没有运算符(例如,添加a和b是(+ a b)),而是函数,可以*定义。它具有强大的类型,即每个对象都有一个明确的类型,即使它在编译时无法知道,转换也受到限制。它是探索性编程的真正优秀语言,听起来就像你将要做的那样。

#12


Ruby does.

require 'rubygems'
require 'superators'

class Array
  superator "<---" do |operand|
    self << operand.reverse
  end
end

["jay"] <--- "spillihp"

#13


You can actually do what you need with C# through operator overloading.

您可以通过运算符重载实际执行C#所需的操作。

Example:

 public static Complex operator -(Complex c)
 {
    Complex temp = new Complex();
    temp.x = -c.x;
    temp.y = -c.y;
    return temp;
 }

#1


Like EFraim said, Haskell makes this pretty easy:

就像EFraim所说,Haskell使这很容易:

% ghci
ghci> let a *-* b = (a*a) - (b*b)
ghci> :type (*-*)
(*-*) :: (Num a) => a -> a -> a
ghci> 4 *-* 3
7
ghci> 1.2 *-* 0.9
0.6299999999999999
ghci> (*-*) 5 3
16
ghci> :{
          let gcd a b | a > b     = gcd (a - b) b 
                      | b > a     = gcd a (b - a) 
                      | otherwise = a
      :}
ghci> :type gcd
gcd :: (Ord a, Num a) => a -> a -> a
ghci> gcd 3 6
3
ghci> gcd 12 11
1
ghci> 18 `gcd` 12
6

You can define new infix operators (symbols only) using an infix syntax. You can then use them as infix operators, or enclose them in parens to use them as a normal function.

您可以使用中缀语法定义新的中缀运算符(仅符号)。然后,您可以将它们用作中缀运算符,或将它们包含在parens中以将它们用作普通函数。

You can also use normal functions (letters, numbers, underscores and single-quotes) as operators by enclosing them in backticks.

您还可以将常规函数(字母,数字,下划线和单引号)用作运算符,方法是将它们括在反引号中。

#2


Well, you can redefine a fixed set of operators in many languages, like C++ or C#. Others, like F# or Scala allow you to define even new operators (even as infix ones) which might be even nicer for math-y stuff.

好吧,你可以用许多语言重新定义一组固定的运算符,比如C ++或C#。其他的,比如F#或Scala,你甚至可以定义新的运算符(即使是中缀运算符),这些运算符甚至可能更好用于数学运算。

#3


Maybe Haskell? Allows you to define arbitrary infix operators.

也许哈斯克尔?允许您定义任意中缀运算符。

#4


Ted Neward wrote a series of article on Scala aimed at Java developers, and he finished it off by demonstrating how to write a mathematical domain language in Scala (which, incidentally, is a statically-typed language)

Ted Neward写了一系列关于Scala的文章,针对Java开发人员,他通过演示如何在Scala中编写数学域语言(顺便提一下,这是一种静态类型的语言)来完成它。

Part 1

Part 2

Part 3

#5


In C++ you can define operators that work on other classes, but I don't think other primitive types like ints since they can't have instance methods. You could either make your own number class in C++ and redefine ALL the operators, including + * etc.

在C ++中,您可以定义适用于其他类的运算符,但我不认为其他原始类型如int,因为它们不能有实例方法。您可以在C ++中创建自己的数字类并重新定义所有运算符,包括+ *等。

To make new operators on primitive types you have to turn to functional programming (it seems from the other answers). This is fine, just keep in mind that functional programming is very different from OOP. But it will be a great new challenge and functional programming is great for math as it comes from lambda calc. Learning functional programming will teach you different skills and help you greatly with math and programming in general. :D

要在原始类型上创建新的运算符,您必须转向函数式编程(从其他答案看来)。这很好,请记住,函数式编程与OOP非常不同。但这将是一个伟大的新挑战,函数式编程对于数学非常有用,因为它来自lambda calc。学习函数式编程将教会您不同的技能,并通常帮助您进行数学和编程。 :d

good luck!

#6


Eiffel allows you to define new operators.

Eiffel允许您定义新的运算符。

http://dev.eiffel.com

#7


Inasmuch as the procedure you apply to the arguments in a Lisp combination is called an “operator,” then yeah, you can define new operators till the cows come home.

因为你应用于Lisp组合中的参数的过程被称为“操作符”,所以是的,你可以定义新的操作符直到奶牛回家。

#8


Ada has support for overriding infix operators: here is the reference manual chapter.

Ada支持覆盖中缀运算符:这是参考手册章节。

Unfortunately you can't create your own new operators, it seems you can only override the existing ones.

遗憾的是,您无法创建自己的新运算符,您似乎只能覆盖现有运算符。

type wobble is new integer range 23..89;

function "+" (A, B: wobble) return wobble is
begin
   ...
end "+";

Ada is not a hugely popular language, it has to be said, but as far as strong typing goes, you can't get much stronger.

不得不说Ada不是一种非常流行的语言,但就强打字而言,你不可能变得更强大。

EDIT:

Another language which hasn't been mentioned yet is D. It also is a strongly typed language, and supports operator overloading. Again, it doesn't support user-defined infix operators.

另一种尚未提及的语言是D.它也是一种强类型语言,并支持运算符重载。同样,它不支持用户定义的中缀运算符。

From http://www.digitalmars.com/d/1.0/rationale.html

Why not allow user definable operators?

为什么不允许用户定义运营商?

These can be very useful for attaching new infix operations to various unicode symbols. The trouble is that in D, the tokens are supposed to be completely independent of the semantic analysis. User definable operators would break that.

这些对于将新的中缀操作附加到各种unicode符号非常有用。麻烦的是,在D中,令牌应该完全独立于语义分析。用户可定义的运营商会打破这种局面。

#9


Both ocaml and f# have infix operators. They have a special set of characters that are allowed within their syntax, but both can be used to manipulate other symbols to use any function infix (see the ocaml discussion).

ocaml和f#都有中缀运算符。它们在语法中有一组特殊的字符,但两者都可用于操纵其他符号以使用任何函数中缀(参见ocaml讨论)。

#10


I think you should probably think deeply about why you want to use this feature. It seems to me that there are much more important considerations when choosing a language.

我想您应该深入思考为什么要使用此功能。在我看来,在选择语言时有更重要的考虑因素。

I can only think of one possible meaning for the word "operator" in this context, which is just syntactic sugar for a function call, e.g. foo + bar would be translated as a call to a function +(a, b).

在这种情况下,我只能想到“运算符”这个词的一个可能含义,它只是函数调用的语法糖,例如, foo + bar将被翻译为对函数+(a,b)的调用。

This is sometimes useful, but not often. I can think of very few instances where I have overloaded/defined an operator.

这有时很有用,但不常见。我可以想到很少有我重载/定义运算符的实例。

As noted in the other answers, Haskell does allow you to define new infix operators. However, a purely functional language with lazy evaluation can be a bit of a mouthful. I would probably recommend SML over Haskell, if you feel like trying on a functional language for the first time. The type system is a bit simpler, you can use side-effects and it is not lazy.

如其他答案中所述,Haskell允许您定义新的中缀运算符。然而,具有懒惰评估的纯函数语言可能有点拗口。如果您想第一次尝试使用函数式语言,我可能会推荐Sask而不是Haskell。类型系统有点简单,你可以使用副作用,它不是懒惰。

F# is also very interesting and also features units of measure, which AFAIK is unique to that language. If you have a need for the feature it can be invaluable.

F#也非常有趣,并且还具有测量单位,AFAIK是该语言独有的。如果您需要该功能,它可能是非常宝贵的。

Off the top of my head I can't think of any statically typed imperative languages with infix operators, but you might want to use a functional language for math programming anyway, since it is much easier to prove facts about a functional program.

在我的脑海中,我无法想到使用中缀运算符的任何静态类型的命令式语言,但你可能想要使用函数式语言进行数学编程,因为它更容易证明关于函数式程序的事实。

You might also want to create a small DSL if syntax issues like infix operators are so important to you. Then you can write the program in whatever language you want and still specify the math in a convenient way.

如果像中缀运算符这样的语法问题对您来说非常重要,您可能还想创建一个小型DSL。然后,您可以用您想要的任何语言编写程序,并以方便的方式指定数学。

#11


What do you mean by strong typing? Do you mean static typing (where everything has a type that is known at compile time, and conversions are restricted) or strong typing (everything has a type known at run time, and conversions are restricted)?

强类型是什么意思?你的意思是静态类型(其中一切都有编译时已知的类型,转换受到限制)或强类型(一切都在运行时已知类型,转换受到限制)?

I'd go with Common Lisp. It doesn't actually have operators (for example, adding a and b is (+ a b)), but rather functions, which can be defined freely. It has strong typing in that every object has a definite type, even if it can't be known at compile time, and conversions are restricted. It's a truly great language for exploratory programming, and it sounds like that's what you'll be doing.

我会选择Common Lisp。它实际上没有运算符(例如,添加a和b是(+ a b)),而是函数,可以*定义。它具有强大的类型,即每个对象都有一个明确的类型,即使它在编译时无法知道,转换也受到限制。它是探索性编程的真正优秀语言,听起来就像你将要做的那样。

#12


Ruby does.

require 'rubygems'
require 'superators'

class Array
  superator "<---" do |operand|
    self << operand.reverse
  end
end

["jay"] <--- "spillihp"

#13


You can actually do what you need with C# through operator overloading.

您可以通过运算符重载实际执行C#所需的操作。

Example:

 public static Complex operator -(Complex c)
 {
    Complex temp = new Complex();
    temp.x = -c.x;
    temp.y = -c.y;
    return temp;
 }