没有用于(Show([(Char,Char)]-> Char)的实例) [英] No instance for (Show ([(Char, Char)] -> Char))

查看:130
本文介绍了没有用于(Show([(Char,Char)]-> Char)的实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我必须创建一个函数,以找到第一个字母对并返回第二个字母。

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

我实际上找到了一个答案,但是使用了map函数和

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

我得到此消息:

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


推荐答案

您的代码很好,您只需要在ghci提示符下提供所有参数,例如

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

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

应该给你'q'。

它抱怨无法显示每当它说它没有
带有-> in的东西的Show实例时,就抱怨它不能
显示一个函数,它只能显示使用该函数的结果

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.

当您提供一些但不是全部数据时,Haskell会将其解释为带有下一个参数的新函数
,因此

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.

顺便说一句,几乎每次您收到 No instance for ...错误,这是因为
您对参数做错了-错过了一些,以错误的顺序将它们放入
。编译器的 try 会通过建议您在实例中添加
来提供帮助,但可能只需要检查您是否以正确的顺序提供了参数
的写类型。

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.

学习Haskell很有趣!

Have fun learning Haskell!

这篇关于没有用于(Show([(Char,Char)]-> Char)的实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆