Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?"

like image 592
niklasfi Avatar asked Jul 26 '09 12:07

niklasfi


People also ask

What is a strongly typed language in programming?

A strongly typed programming language is one in which each type of data, such as integers, characters, hexadecimals and packed decimals, is predefined as part of the programming language, and all constants or variables defined for a given program must be described with one of the data types.

What is the most strongly typed language?

Smalltalk, Ruby, Python, and Self are all "strongly typed" in the sense that typing errors are prevented at runtime and they do little implicit type conversion, but these languages make no use of static type checking: the compiler does not check or enforce type constraint rules.

Why is C not strongly typed?

C is statically but weakly typed: The weakly type system allows some freedom for speed improvements and allows to handle the memory at a low level. It is thus perfectly fine to use it when you know what you are doing, for tasks where the memory footprint and speed are important.

Can Python be strongly typed?

Python is both a strongly typed and a dynamically typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a variable. Dynamic typing means that the type of the variable is determined only during runtime.


2 Answers

Like EFraim said, Haskell makes this pretty easy:

% 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.

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

like image 62
rampion Avatar answered Oct 20 '22 00:10

rampion


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.

like image 40
Joey Avatar answered Oct 19 '22 23:10

Joey