3

i'm playing a little with Haskell and i'm stuck with this error, using the snd function with a tuple (String, list).

snd ("Felix Felices",[("Escarabajos Machacados",52,[f1,f2]),("Ojo de Tigre Sucio",2,[f2])])
ERROR - Cannot find "show" function for:
*** Expression : snd ("Felix Felices",[("Escarabajos Machacados",52,[f1,f2]),("Ojo de Tigre Sucio",2,[f2])])
*** Of type    : [([Char],Integer,[(Integer,Integer,Integer) -> (Integer,Integer,Integer)])]

The thing is, if i remove f1, f2 and f3 (they are functions) the code works just fine, it returns the list. Why is this happening, why can't i just put a function inside the tuple's second position?

ferostar
  • 7,074
  • 7
  • 38
  • 61

4 Answers4

5

You can put functions into tuples. But you can't display them - ghci wants to show the result and print it. How should it show (convert to string) the functions? It can't, or at least nobody felt like choosing one way of doing it (which would propably be flawed anyway - at least I can't think of any approach that doesn't have holes to huge even I can see them). Therefore, you can't evaluate something that returns functions or collections of functions in ghci.

4

The problem isn't really the functions f1 f2 or f3, the problem is that you are trying to print these functions, but functions don't have an instance of Show, so they cannot be printed. However if you try:

Prelude> snd ("Felix Felices",[("Escarabajos Machacados",52,["f1","f2"]),("Ojo de Tigre     Sucio",2,["f2"])])

you get the result:

[("Escarabajos Machacados",52,["f1","f2"]),("Ojo de Tigre Sucio",2,["f2"])]

So the problem isn't that you cannot have a function in a tuple, the problem is that you cannot convert functions to strings so they can be printed.

HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
  • Thanks for all the answers! So, is it possible to define (i was going to say override...) the Show function inside f1, f2 and f3 in order to show, for example, their names? – ferostar Feb 17 '11 at 20:55
  • No, because Haskell doesn't work like that. Haskell works with expressions, which are evaluated to values. "Show" is a typeclass, which is not the same as a Java class. See here for more details: http://stackoverflow.com/questions/2685626/explain-type-classes-in-haskell/2686021#2686021 – Paul Johnson Feb 17 '11 at 22:16
3

module Text.Show.Functions provides an instance Show (a -> b).

ghci> :m +Text.Show.Functions
ghci> [(*), (/)]
[<function>,<function>]

It's not useful for actually figuring out what the functions are, but there's no good way to do that anyway (well, the debugger and vacuum aside). But if you just want some Show instances for convenience, this is in the standard library.

ephemient
  • 198,619
  • 38
  • 280
  • 391
0

You cannot print bare functions in Haskell as there is no "show" function defined for them. You get the same type of error, if you type

Hugs> sqrt

for example

The system tells the type of the expression, which in your case is ([Char],Integer,[(Integer,Integer,Integer) -> (Integer,Integer,Integer)])], but cannot print it, because it is a function.

Timo
  • 5,188
  • 6
  • 35
  • 38