Haskell and other languages

I have just gone through my old project which I have on my GitHub repository. I put there all of my micro projects which I wrote for learning purpose or just for a fun. There are projects in common languages like C/C++, java, C#, Delphi and also projects in some extraordinary like assembler, TCL or functional programming language Haskell.
It is interesting how simple code could be calculation of derivative of polynomial:

-- triming polynomial
trimPolynomial :: [Int] -> [Int]
trimPolynomial p = if null p then [] else if (last p) /= 0 then p else trimPolynomial (init p)

-- degree of polynomial
polynomialDegree :: [Int] -> Int
polynomialDegree p = length (trimPolynomial p) - 1

-- basic polynomial functions - add, sub
term :: [Int] -> Int -> Int
term p i = if i < (length p) then p!!i else 0

addition :: [Int] -> [Int] -> [Int]
addition p1 p2 = trimPolynomial [(term p1 i) + (term p2 i) | i <- [0..(max (length p1) (length p2)) - 1]]

subtraction :: [Int] -> [Int] -> [Int]
subtraction p1 p2 = trimPolynomial [(term p1 i) - (term p2 i) | i <- [0..(max (length p1) (length p2)) - 1]]

-- polynomial first derivative
derivative :: [Int] -> [Int]
derivative p = trimPolynomial [(i * (p!!i)) | i <- [1..(length (trimPolynomial p)) - 1]]

derivativeByX :: [Int] -> Int -> [Int]
derivativeByX p 0 = p
derivativeByX p n = derivative (derivativeByX p (n - 1))

-- printing of polynomial
showTerm :: Int -> Int -> String
showTerm 0 i = ""

If you are interesting in my old code, just visit my github project Examples

That’s all, now go write some code.

Leave a Reply

Your email address will not be published. Required fields are marked *