mifun s = foldr op 0 s
where op x r = head x + r
ghciに教えてもらう方法はありますか?
try :t mifun
(の略:type mifun
)
これは
*Main> :t mifun
mifun :: (Num b) => [[b]] -> b
したがって、b
のインスタンスの場合、num
のリストのリストを取得し、単一mifun
のリストを出力します(この場合はリストの最初の要素の合計です)。b
b
これは実際には答えではありませんが、フォーマットが必要でした。
注意: mifun
含まれるリストのいずれかが空の場合は ⊥ です。例えば:
> mifun [[3], [5, 8], [], [1, 2, 3]]
*** Exception: Prelude.head: empty list
上記の例の結果を 9 にしたい (空のリストを合計に寄与しないものとして扱う) 場合は、次のいずれかの方法で op を定義する必要があります。
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
私はおそらく最初の方が好きです。