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

巧了我就是萌 提交于 2019-11-30 22:42:49

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!

It seems that you typed something like this in ghci:

*Main> lookUp 'c'

An expression like lookUp 'c' is a partial evaluation / curried form of the lookUp function. It's type is:

*Main> :t lookUp 'c'
lookUp 'c' :: [(Char, Char)] -> Char

which is the exact type that ghci says there is no Show instance for.

To test your function, be sure to supply both x and the the list of Char pairs:

*Main> lookUp 'c' [ ('a','A'), ('b','B'), ('c','C') ]
'C'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!