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

后端 未结 2 1990
闹比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!

提交回复
热议问题