我是编程的新手,我被要求将3个haskell函数转换为python作为练习练习.3个功能是连接的,因为一个的输出用作下一个的输入,依此类推.
我得到了haskell函数的功能,但我不知道如何开始转换它们!
这是haskell代码:
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)
Run Code Online (Sandbox Code Playgroud)
这是我转换它的尝试:
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 != …Run Code Online (Sandbox Code Playgroud) 我知道Python有一些惰性实现,因此,我想知道是否可以在Python中使用循环编程。
如果不是,为什么?