I am new to programming and I was asked to convert 3 haskell functions in python as an exercise. 3 functions are connected, since the output of one of them is used as the input of the next, etc.
I get what haskell functions do, but I don't know how to start converting them!
This is the haskell code:
factorial :: Int -> Int factorial n = product (down n) product :: [Int] -> Int product [] = 1 product (a:as) = a * product as down :: Int -> [Int] down 0 = [] down n = n : down (n-1)
And it was my attempt to convert it:
class function: def down(self): if self.n == 0: self.lista = [] else: self.lista = range(self.n, 0, -1) def product(self): for x in self.lista: if x == []: self.product = 1 if x != []: for i in self.lista: self.product = i * self.product def factorial(self): self.n = int(raw_input("n=")) self.fact = self.product(self.down(self.n)) print self.fact raw_input() c = function() c.factorial()
Well, at first I think this is not a โdirect conversionโ of haskell code. It will be good, but, secondly, it will not work.
This lack of background programming is killing me ... can someone help me with this problem?
Thanks a lot!
Edit:
The goal of this problem is to accurately convert haskell to python. I made my personal version, it was the next step in the exercise ^^
source share