您好: 我有一个函数,它获取一个字符串,并关于它得到什么,它调用一些其他函数。除了其中一个之外,其他所有人都不需要参数。但是那个需要它的人期望得到一个由我定义的类型的论证。我的意图是要求输入通过。但是,使用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的新手
非常感谢你。
此致
答案 0 :(得分:1)
看起来他正试图跟踪球员(NombreJug =球员姓名),PuntosLogrados(获得积分)和上场时间(MinutosJugados),然后通过某些标准找到最佳球员。
armarListaDeTuplasPuntosFecha抛弃了上场时间,以返回一个玩家名字和分数的元组。
mejorJugadorPor(“最佳玩家”)试图询问用户输入列表,然后选择得分最高的玩家。我认为你是对的,他需要一个类型的Read实例,或者一个解析输入并将其转换为顶部定义的Fecha类型的函数。它还取决于如何定义NombreJug,PuntosLogrados,MinutosJugados。他们是同义词吗?
mejorJugadorPor看起来也应该是String-&gt;类型; IO NombreJug,因为它执行IO操作。
这是我尝试做你想做的事情:
import Data.List
type NombreJug = String
type PuntosLogrados = Int
type MinutosJugados = Int
type Fecha = [(NombreJug,PuntosLogrados,MinutosJugados)]
armarListaDeTuplasPuntosFecha::Fecha->[(NombreJug,PuntosLogrados)]
armarListaDeTuplasPuntosFecha = map desechar
where desechar (x,y,_) = (x,y)
jugadorConMayorCantidadDePuntoEnFecha unaFecha = fst (maximumBy mayorTupla (armarListaDeTuplasPuntosFecha unaFecha))
mayorTupla = undefined
mejorJugadorPor:: String -> IO NombreJug
mejorJugadorPor criterio
| criterio == "Mayor Cantidad de puntos en fecha" = do
fecha <- readLn
return $ jugadorConMayorCantidadDePuntoEnFecha fecha
| otherwise = return "No es un criterio valido, reintente una proxima vez"
我添加了“mayorTupla = undefined”来编译它,因为你发布的代码中没有定义该函数。
我所做的改变:
你的函数armarListaDeTuplasPuntosFecha更好用map表示。 Map将函数应用于列表的每个元素,这是您手动执行的操作。
jugadorConMayorCantidadDePuntoEnFecha可以用fst表示,它返回两个值的元组的第一个元素
mejorJugadorPor需要在IO monad中,因为它执行输入/输出操作(读取用户输入的内容)。你可以通过将返回类型从String更改为IO String来表示返回值取决于IO(即函数不是纯粹的)。
函数readLn可以执行您想要的操作,因为只要类型具有Read实例,它就会将输入字符串转换为正确的类型。 Read类通常意味着您可以以某种方式将字符串转换为类型的值。
因为mejorJugadorPor是monadic,所以你需要确保它返回的值包含在IO monad中。这是函数返回的作用:它采用类型“a”的值并将其转换为“m a”类型的值,其中m是任何monad。
答案 1 :(得分:0)
从我可以收集的内容中,您希望创建Read类的数据类型实例,然后使用read函数将字符串数据读入您的数据类型。
如果那不是您的想法让我知道。
答案 2 :(得分:0)
几个小时之后,我找到了这个烂摊子:在英国的帮助下(Julian Porter:www.jpembedded.co.uk,www.porternet.org)。有办法不创建monad或修改类(我还没达到那个级别):
import Data.List
type NombreJug = String
type NombrePart = String
type Club = String
type Posicion = String
type Cotizacion = Integer
type PuntosLogrados = Integer
type MinutosJugados = Integer
type Jugador = (NombreJug,Club,Posicion,Cotizacion)
type Jugadores = [Jugador]
type PartConSusJug = (NombrePart,[NombreJug])
type Participantes = [PartConSusJug]
type Fecha = [(NombreJug,PuntosLogrados,MinutosJugados)]
type Fechas = [Fecha]
participantes = [("Natalia", ["Abbondazieri","Lluy","Battaglia", "Lazzaro"]),
("Romina", ["Islas", "Lluy", "Battaglia", "Lazzaro"]),
("Jessica", ["Islas"])
]
clubes = ["Boca", "Racing", "Tigre"]
jugadores = [("Abbondazieri", "Boca", "Arquero", 6500000),
("Islas", "Tigre", "Arquero", 5500000),
("Lluy", "Racing", "Defensor", 1800000),
("Battaglia", "Boca", "Volante", 8000000),
("Lazzaro", "Tigre", "Delantero", 5200000),
("Monzon","Boca","Defensor",3500000),
("Guzman","Newells","Arquero",1000000),
("Diaz","Velez","Defensor",3600000),
("Palermo","Boca","Delantero",12000000),
("Aguirre","Lanus","Volante",4500000),
("Cura","Huracan","Defensor",1700000),
("Espinoza","Gimnasia","Volante",300000),
("Clemente","Deportivo Piraña","Volante",60000000)
]
miListaTuplasFechas = [("quinta",[("Lluy", 8, 90),("Lazzaro", 6, 90)]),("sexta",[("Lazzaro", 7, 77),("Islas", 6, 90),("Lluy", 7, 90)]),("septima",[("Battaglia", 13, 90), ("Lluy", 6, 90), ("Lazzaro", 8, 77)]),("octava",[("Islas", 4, 84), ("Battaglia", 8, 90)])]
fechas = [quinta, sexta, septima, octava]
quinta = [("Lluy", 8, 90), ("Lazzaro", 6, 90)]
sexta = [("Lazzaro", 7, 77), ("Islas", 6, 90), ("Lluy", 7, 90)]
septima = [("Battaglia", 13, 90), ("Lluy", 6, 90), ("Lazzaro", 8, 77)]
octava = [("Islas", 4, 84), ("Battaglia", 8, 90)]
-- 10) mejorJugadorPor, recibe un criterio y devuelve el mejor jugador de acuerdo a ese criterio.
-- Dar además ejemplos de consultas que resuelvan los siguientes requerimientos:
mayorTupla (n1, c1) (n2, c2)
| c1 > c2 = GT
| c1 <= c2 = LT
-- 1.el jugador que logro mayor cantidad de puntos en todo el torneo. -> "Lazzaro"
armarListaDeTuplasPuntos::Jugadores->[(NombreJug,PuntosLogrados)]
armarListaDeTuplasPuntos [] = []
armarListaDeTuplasPuntos (ej:ejs) = [ (((\ (nombre,_,_,_)-> nombre) ej), (totalPuntosJugador ((\ (nombre,_,_,_)-> nombre) ej))) ] ++ armarListaDeTuplasPuntos ejs
mostrarmeLasTuplasPuntos = armarListaDeTuplasPuntos jugadores
jugadorConMayorCantidadDePuntosEnTorneo = (\ (nombre,puntos)->nombre) (maximumBy mayorTupla mostrarmeLasTuplasPuntos)
-- 2.el jugador que posee la mayor cotización.-> "Battaglia"
armarListaDeTuplasCotizacion::Jugadores->[(NombreJug,Cotizacion)]
armarListaDeTuplasCotizacion [] = []
armarListaDeTuplasCotizacion (ej:ejs) = [((\ (nombre,_,_,cotizacion)-> (nombre,cotizacion)) ej)] ++ armarListaDeTuplasCotizacion ejs
mostrarmeLasTuplasCotizaciones = armarListaDeTuplasCotizacion jugadores
jugadorConLaMayorCotizacion = (\ (nombre,cotizacion)->nombre) (maximumBy mayorTupla mostrarmeLasTuplasCotizaciones)
--Aquí se ve un ejemplo de aplicación de orden superior: la función maximumBy recibe dos funciones como agumentos.
-- 3.el jugador que logro mayor cantidad de puntos en una fecha. (en la 5º) -> "Lluy"
armarListaDeTuplasPuntosFecha::Fecha->[(NombreJug,PuntosLogrados)]
armarListaDeTuplasPuntosFecha [] = []
armarListaDeTuplasPuntosFecha (ej:ejs) = [((\ (nombre,puntos,_)-> (nombre,puntos)) ej)] ++ armarListaDeTuplasPuntosFecha ejs
jugadorConMayorCantidadDePuntoEnFecha [] = "Fecha no definida"
jugadorConMayorCantidadDePuntoEnFecha unaFecha = (\ (nombre,puntos)->nombre) (maximumBy mayorTupla (armarListaDeTuplasPuntosFecha unaFecha))
-- 4.el jugador que logro el mejor promedio en todo el torneo. -> "Battaglia"
armarListaDeTuplasPromedios::Jugadores->[(NombreJug,Float)]
armarListaDeTuplasPromedios [] = []
armarListaDeTuplasPromedios (ej:ejs) = [ (((\ (nombre,_,_,_)-> nombre) ej), (promedioPuntosJugador ((\ (nombre,_,_,_)-> nombre) ej))) ] ++ armarListaDeTuplasPromedios ejs
mostrarmeLasTuplasPromedios = armarListaDeTuplasPromedios jugadores
jugadorConMejorPromedioDelTorneo = (\ (nombre,puntos)->nombre) (maximumBy mayorTupla mostrarmeLasTuplasPromedios)
--Aquí se ve un ejemplo de aplicación de orden superior: la función mostrarmeLasTuplasPromerios es pasada como parámetro a la expresión lambda.
otroCaso = "No es un criterio valido, reintente una proxima vez"
listaDeCriterios criterio | (criterio == "jugadorConMayorCantidadDePuntosEnTorneo") = jugadorConMayorCantidadDePuntosEnTorneo
| (criterio == "jugadorConLaMayorCotizacion") = jugadorConLaMayorCotizacion
| (criterio == "jugadorConMejorPromedioDelTorneo") = jugadorConMejorPromedioDelTorneo
| ((criterio /= "jugadorConMayorCantidadDePuntosEnTorneo")&& (criterio /= "jugadorConLaMayorCotizacion")&&(criterio /= "jugadorConMejorPromedioDelTorneo")) = otroCaso
devolverFecha::String->[(String,Fecha)]->Fecha
devolverFecha laFecha [] = []
devolverFecha laFecha (f:fs) | (((\ fechaIngresada (fechaAComparar,_)-> fechaIngresada == fechaAComparar) laFecha f) == True) = snd f
| otherwise = devolverFecha laFecha fs
criterios1 = do
putStrLn "Ingrese la fecha deseada: "
x<-getLine
let resultado = ((jugadorConMayorCantidadDePuntoEnFecha (devolverFecha x miListaTuplasFechas)))
putStrLn ("\""++resultado++"\"")
criterios2::String->IO ()
criterios2 criterio = do
let resultado = (listaDeCriterios criterio)
putStrLn ("\""++resultado++"\"")
eleccionDeCriterios criterioElegido | (criterioElegido == "jugadorConMayorCantidadDePuntoEnFecha") = criterios1
| otherwise = criterios2 criterioElegido
mejorJugadorPor = do
putStrLn "Por favor, ingrese un criterio: "
criterio<-getLine
eleccionDeCriterios criterio
控制台输出:
Main> mejorJugadorPor
Por favor, ingrese un criterio:
jugadorConMejorPromedioDelTorneo
"Battaglia"
Main> mejorJugadorPor
Por favor, ingrese un criterio:
pepe
"No es un criterio valido, reintente una proxima vez"
Main>
Main>
Main> mejorJugadorPor
Por favor, ingrese un criterio:
jugadorConMayorCantidadDePuntoEnFecha
Ingrese la fecha deseada:
quinta
"Lluy"
Main> mejorJugadorPor
Por favor, ingrese un criterio:
jugadorConMayorCantidadDePuntoEnFecha
Ingrese la fecha deseada:
decima
"Fecha no definida"
用西班牙语。如果有人发现它有用,请与我联系,我会将其翻译成英文。
非常感谢那些对此问题发表评论的人以及他们的建议。