Haskell
Public
Basic Haskell Math and Utility Functions
Defines several mathematical and utility functions in Haskell including calculations for circle area, sphere volume, temperature conversion, salary raise percentage, minimum of two numbers, absolute value, sign determination, case swapping for characters, factorial, and summations over ranges.
Haskell
module Gyak04 where
import Data.Char(toLower, toUpper, isLower, isUpper)
circleArea :: Double -> Double
circleArea r = (r^2)*pi
sphereVolume :: Double -> Double
sphereVolume r = 4*(r^3)*pi/3
cToF :: Double -> Double
cToF c = c*(9/5)+32
raiseSalary :: Double -> Double -> Double
raiseSalary x y = ((x/y)-1)*100
min1 :: Int -> Int -> Int
min1 x y
| x <= y = x
| y < x = y
min2 :: Int -> Int -> Int
min2 x y
|x <= y = x
|otherwise = y
min3 :: Int -> Int -> Int
min3 x y | x <= y = x
min3 x y = y
myAbs :: Integer -> Integer
myAbs x
| x >= 0 = x
| x < 0 = -x
sign :: Integer -> Integer
sign x
| x < 0 = -1
| x == 0 = 0
| x > 0 = 1
swapUpperLower :: Char -> Char
swapUpperLower x
|isUpper(x) == True = toLower(x)
|isUpper(x) == False = toUpper(x)
fact :: Integer -> Integer
fact x
| x == 0 = 1
| x > 0 = x * fact(x-1)
| otherwise = error "fact: negative number"
sumTo :: Integer -> Integer
sumTo 0 = 0
sumTo n = n + sumTo (n-1)
sumBetween :: Integer -> Integer -> Integer
sumBetween n m
| n > m = sumTo(n) - sumTo(m-1)
| m > n = sumTo(m) - sumTo(n-1)
| m == n = m