小编use*_*887的帖子

在Haskell中将列表元素相乘

我想要一个函数,它接受列表的内容的产品并复制其元素.

例如,列表是:[2, 3, 4, 5].

其中的产品:[1, 2, 6, 24, 120].

最后,列表应如下所示:[1, 1, 2, 2, 2, 6, 6, 6, 6, 24, 24, 24, 24, 24].

我的问题是[1, 2, 6, 24, 120]不应该改变,但我无法解决它,我对haskell很新.您不需要修改此代码,您可以创建一个新代码.

makeSystem :: Integral a => [a] -> [a]
makeSystem l= replicate (l !! 0) ((map product(inits l))!!0) ++ asd (tail l) where
 inits [] = [[]]
 inits (x:xs) = [[]] ++ map (x:) (inits xs)
Run Code Online (Sandbox Code Playgroud)

另一个例子: makeSystem [5,2,5,2,5,2]

结果: [1, 1, 1, 1, …

haskell product list

3
推荐指数
1
解决办法
1467
查看次数

标签 统计

haskell ×1

list ×1

product ×1