Question

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

Is there a way to make ghci tell me?

Was it helpful?

Solution

try :t mifun (short for :type mifun)

which gives

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

So, for a b an instance of num, mifun takes a list of lists of b and outputs a single b (which in this case is the sum of the first elements of the lists).

OTHER TIPS

This isn't really an answer, but I needed the formatting.

N.B.: mifun is ⊥ if any of the contained lists is empty. For example:

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

If you want the result of the above example to be 9 (treating an empty list as not contributing to the sum), then you should define op as one of the following ways:

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 

I'd probably prefer the first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top