将 String 转换为用户定义类型。输入输出问题
你好: 我有一个获取字符串的函数,并且关于它获取的内容,它调用了一些其他函数。除了其中之一之外,所有这些都不需要争论。但确实需要它的人期望收到一个由我定义类型的参数。我的意图是需要输入才能通过。但是,使用 getLine、getChar、getInt 存储输入并保持类型([Char]、Char 等),并且我需要将粗略输入传递给该函数,以便推断系统能够检测到其类型是我的用户 -定义类型(Fecha)。
代码摘录:
type Fecha = [(NombreJug,PuntosLogrados,MinutosJugados)]
armarListaDeTuplasPuntosFecha::Fecha->[(NombreJug,PuntosLogrados)]
armarListaDeTuplasPuntosFecha [] = []
armarListaDeTuplasPuntosFecha (ej:ejs) = [((\ (nombre,puntos,_)-> (nombre,puntos)) ej)] ++ armarListaDeTuplasPuntosFecha ejs
**jugadorConMayorCantidadDePuntoEnFecha unaFecha** = (\ (nombre,puntos)->nombre) (maximumBy mayorTupla (armarListaDeTuplasPuntosFecha unaFecha))
mejorJugadorPor::String->NombreJug
mejorJugadorPor criterio | criterio == "Mayor Cantidad de puntos en fecha" = do
fecha<-getLine
jugadorConMayorCantidadDePuntoEnFecha (fecha)
| otherwise = "No es un criterio valido, reintente una proxima vez"
如果您能帮助我解决这个问题,我将非常感激。由于我是 Haskell 的新手,我发现可用的文档对我来说还不够
。提前非常感谢。
问候
Hello:
I have a function which gets a string, and regarding what it gets, it calls some other functions. All but one of them, do not needs arguments. But the one that do needs it expect to receive an argument which type is defined by me. My intention is to require input to pass. But, using getLine, getChar, getInt, store the input keeping the type ([Char],Char,etc), and I need to pass rough input to that function so the inferring system is able to detect that its type is my user-defined type (Fecha).
Extracts from code:
type Fecha = [(NombreJug,PuntosLogrados,MinutosJugados)]
armarListaDeTuplasPuntosFecha::Fecha->[(NombreJug,PuntosLogrados)]
armarListaDeTuplasPuntosFecha [] = []
armarListaDeTuplasPuntosFecha (ej:ejs) = [((\ (nombre,puntos,_)-> (nombre,puntos)) ej)] ++ armarListaDeTuplasPuntosFecha ejs
**jugadorConMayorCantidadDePuntoEnFecha unaFecha** = (\ (nombre,puntos)->nombre) (maximumBy mayorTupla (armarListaDeTuplasPuntosFecha unaFecha))
mejorJugadorPor::String->NombreJug
mejorJugadorPor criterio | criterio == "Mayor Cantidad de puntos en fecha" = do
fecha<-getLine
jugadorConMayorCantidadDePuntoEnFecha (fecha)
| otherwise = "No es un criterio valido, reintente una proxima vez"
I would really appreciate if you can help me with this issue. The available documentation I've found its insufficient for me due to I'm a rookie with Haskell
Thank you very much in advance.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来他正在尝试跟踪球员(NombreJug = 球员姓名)、PuntosLogrados(获得的分数)和上场时间(MinutosJugados),然后根据某些标准找到最佳球员。
armarListaDeTuplasPuntosFecha 丢弃比赛时间以返回球员姓名和分数的元组。
mejorJugadorPor(“最佳玩家”)试图向用户询问输入列表,然后选择得分最高的玩家。我认为你是对的,他需要一个适合他的类型的 Read 实例,或者一个函数来解析输入并将其转换为顶部定义的 Fecha 类型。它还取决于 NombreJug、PuntosLogrados、MinutosJugados 的定义方式。它们是类型同义词吗?
mejorJugadorPor 看起来也应该是 String-> 类型IO NombreJug,因为它执行 IO 操作。
这是我尝试做你想做的事情:
我添加了“mayorTupla = undefined”来编译它,因为你发布的代码中没有定义该函数。
我所做的更改:
你的函数armarListaDeTuplasPuntosFecha用map可以更好地表达。 Map 将函数应用于列表的每个元素,这就是您手动执行的操作。
jugadorConMayorCantidadDePuntoEnFecha 可以用 fst 表示,它返回两个值的元组的第一个元素
mejorJugadorPor 需要是在 IO monad 中,因为它执行输入/输出操作(读取用户键入的内容)。您可以通过将返回类型从 String 更改为 IO String 来实现此目的,也就是说返回值取决于 IO(即该函数不是纯函数)。
函数 readLn 可以执行您想要的操作,因为只要该类型具有 Read 实例,它就会将输入字符串转换为正确的类型。 Read 类型类基本上意味着您可以以某种方式将字符串转换为该类型的值。
因为mejorJugadorPor是monadic,所以你需要确保它返回的值包含在IO monad中。这就是函数 return 的作用:它接受类型“a”的值并将其转换为类型“m a”的值,其中 m 是任意 monad。
It looks like he is trying to keep track of players (NombreJug = player name), PuntosLogrados (points gained) and playing times (MinutosJugados) and then find the best player by some criteria.
armarListaDeTuplasPuntosFecha throws away the playing times to return a tuple of player name and points.
mejorJugadorPor ("Best player by") is trying to ask the user for a list of inputs and then select the player with the highest score. I think you are right that he needs a Read instance for his type, or a function to parse the input and turn it into type Fecha defined at the top. It also depends on how NombreJug,PuntosLogrados,MinutosJugados are defined. Are they type synonyms?
mejorJugadorPor also looks like it should be of type String-> IO NombreJug, since it performs IO actions.
This is my attempt to do what you want:
I added "mayorTupla = undefined" to get it to compile, because that function isn't defined in the code you posted.
Changes I made:
your function armarListaDeTuplasPuntosFecha is better expressed with map. Map applies a function to every element of a list, which is what you are doing manually.
jugadorConMayorCantidadDePuntoEnFecha can be expressed with fst, which returns the first element of a tuple of two values
mejorJugadorPor needs to be in the IO monad because it performs input/output actions (reading something in that the user types). You do this by change the return type from String to IO String, to say that the return value depends on IO (ie the function isn't pure).
The function readLn does what you want, because it converts the input string to the correct type as long as the type has an instance of Read. The Read type class basically means that you can convert a string into a value of the type somehow.
Because mejorJugadorPor is monadic, you need to make sure that the value it returns is contained in the IO monad. This is what the function return does: it takes a value of type "a" and turns it into a value of type "m a", where m is any monad.
据我所知,您希望创建 Read 类,然后使用 read 函数将字符串数据读入您的数据类型。
如果这不是您的想法,请告诉我。
From what I could gather you want to make your Data types instances of the Read class and then use the read function to read string data into your datatypes.
If that was not what you had in mind let me know.
几个小时后,我在英国的帮助下解决了这个烂摊子(Julian Porter:www.jpeembedded.co.uk,www.porternet.org)。找到了不创建 monad 或修改类的方法(我还没有达到那个级别):
控制台输出:
它是西班牙语。如果有人觉得它有用,请联系我,我会将其翻译成英文。
非常感谢那些对此问题发表评论的人以及他们的建议。
After several hours I got around the mess: with help from UK (Julian Porter: www.jpembedded.co.uk, www.porternet.org). Got the way not to create monads or modifying classes (I'm not at that level yet):
Console output:
It's in Spanish. If somebody finds it useful, contact me and I'll translate it into English.
Thank you very much for those who commented on this issue, and for their recommendations.