Search code examples
mathprogramming-languagesoperators

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


Solution

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