Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Newbie: No instance for Show in map function

In GHCI, I type the following:

map (+1) [1..10]

which returns [2,3,4,5,6,7,8,9,10,11]

So far so good.

Now I type:

min (map (+1) [1..10])

and I get the following error message:

No instance for (Show ([b0] -> [b0]))
  arising from a use of `print'
Possible fix: add an instance declaration for (Show ([b0] -> [b0]))
In a stmt of an interactive GHCi command: print it

This is very strange to me. Why does Haskell think I'm trying to print any arguments, and how do I fix this?

like image 513
Bylextor Avatar asked Jan 14 '12 19:01

Bylextor


1 Answers

The problem is that min takes two arguments (and returns the minimum of the two), but you've only specified one; you want minimum, the version that works on lists.

Specifically, this error occurs because GHCi tries to print out the result of the expressions you evaluate, but in this case, you've applied min to one argument, producing another function.1 GHCi dutifully tries to print this out, but it fails, because functions are not an instance of Show, the standard type-class for things that can be represented as strings for display.

1 In Haskell, all functions take one argument, and functions of multiple arguments are built up from functions returning other functions; for instance, a function adding two integers might have the type Integer -> Integer -> Integer, which is Integer -> (Integer -> Integer) — a function taking an Integer and returning another function which itself takes an Integer, and returns an Integer. Thankfully, you don't have to think about this in such explicit terms very often, or it'd get very confusing!

like image 190
ehird Avatar answered Sep 29 '22 16:09

ehird