There is no instance for (Show ([(Char, Char)] & # 8594; Char))

So, I have to create a function that will find a pair with its first letter and return the second letter.

I really found one answer, but with a map function, and I could not 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 
+4
source share
2 answers

Your code is fine, you just need to specify all the arguments in the ghci prompt, for example

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

Gotta give you a q.

He complains that he cannot show the function. Every time he says that this is not an instance of Show for something with → in, he complains that he cannot show the function. It can only display the result of using a function for some data.

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

 lookUp 'c' 

is a function that takes a list of pairs of characters and gives you a character. This is what he tried to show, but could not.

By the way, almost every time you get the error "There is no instance for ...", this is because you did something wrong with the arguments - missed some of them, put them in the wrong order. The compiler is trying to be helpful by suggesting you add an instance, but you probably just need to verify that you provided the record type of the arguments in the correct order.

Have fun learning Haskell!

+8
source

It seems like you typed something like this in ghci:

 *Main> lookUp 'c' 

An expression like lookUp 'c' is a partial evaluation / currency form of the lookUp function. His type:

 *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 specify both x and a list of Char pairs:

 *Main> lookUp 'c' [ ('a','A'), ('b','B'), ('c','C') ] 'C' 
+4
source

Source: https://habr.com/ru/post/1438783/


All Articles