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

后端 未结 2 1989
闹比i
闹比i 2021-01-06 05:48

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\'

相关标签:
2条回答
  • 2021-01-06 05:53

    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!

    0 讨论(0)
  • 2021-01-06 06:00

    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'
    
    0 讨论(0)
提交回复
热议问题