mifun s = foldr op 0 s
          where op x r = head x + r 

有没有办法让ghci的告诉我?

有帮助吗?

解决方案

:t mifun(简称:type mifun

其给出

*Main> :t mifun
mifun :: (Num b) => [[b]] -> b

因此,对于b num的一个实例,需要mifunb列表的列表并且输出单个b(在这种情况下是所述列表中的所述第一元素的总和)。

其他提示

这是不是一个真正的答案,但我需要的格式。

<强> N.B: mifun是⊥如果任何所包含的列表是空的。例如:

> mifun [[3], [5, 8], [], [1, 2, 3]]
*** Exception: Prelude.head: empty list

如果要在上述实施例的结果是9(处理空列表作为没有贡献的总和),则你应该定义运算为下列方式之一:

mifun s = foldr op 0 s
          where op []    r = r
                op (x:_) r = x + r 

mifun s = foldr op 0 s
          where op x r = (if null x then 0 else head x) + r 

mifun s = foldr op 0 s
          where op x r = sum (take 1 x) + r 

我可能喜欢所述第一

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top