Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No instance for (Show ([(Char, Char)] -> Char))

So I have to make a function that finds a pair with its 1st letter and returning the 2nd letter.

I actually found one answer but with the map function and I couldn't get it.

      lookUp :: Char -> [(Char, Char)] -> Char
      lookUp x [] = x
      lookUp x ( ( st,nd ): rst) | st == x = nd
                   | otherwise = lookUp x rst

And I get this message:

No instance for (Show ([(Char, Char)] -> Char))
arising from a use of `print'
 Possible fix:
  add an instance declaration for (Show ([(Char, Char
  In a stmt of an interactive GHCi command: print it
like image 486
Nick Avatar asked Dec 11 '22 21:12

Nick


1 Answers

Your code is fine, you just need to supply all the arguments at the ghci prompt, eg

lookUp 'c' [('b','n'), ('c','q')]

Should give you 'q'.

It's complaining that it can't show a function. Any time it says it doesn't have a Show instance for something with -> in, it's complaining it can't show a function. It can only show the result of using the function on some data.

When you give it some, but not all data, Haskell interprets that as a new function that takes the next argument, so

lookUp 'c'

is a function that will take a list of pairs of characters and give you a character. That's what it was trying to show, but couldn't.

By the way, almost every time you get a "No instance for..." error, it's because you did something wrong with the arguments - missed some out, put them in the wrong order. The compiler's trying to be helpful by suggesting you add an instance, but probably you just need to check you supplied the write type of arguments in the right order.

Have fun learning Haskell!

like image 96
AndrewC Avatar answered Dec 29 '22 20:12

AndrewC