2011-02-15 45 views
0

你好: 我有一個函數,它獲取一個字符串,並且關於它得到什麼,它調用一些其他函數。除了其中一個以外,都不需要論證。但是,需要它的人會收到一個由我定義的類型的參數。我的意圖是要求輸入通過。但是,使用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

從我能收集你想使Read類的數據類型的實例,然後使用讀函數讀取字符串數據到您的數據類型。

如果這不是你想到的讓我知道。

+0

不確切。我沒有在Haskell中使用類。我是新來的哈斯克爾。這是試圖加載.hs文件時顯示的問題。當我嘗試加載.hs文件時,顯示一個錯誤:***表達式:標準== jugadorConMayorCantidadDePuntoEnFecha ***術語:標準 ***類型:[字符] ***不符合: > NombreJug – Gideo 2011-02-16 16:51:00

1

看起來他試圖跟蹤球員(NombreJug =球員姓名),PuntosLogrados(積分增加)和上場時間(MinutosJugados),然後通過某些標準找到最好的球員。

armarListaDeTuplasPuntosFecha拋出播放時間返回一個玩家名稱和點的元組。 (「Best player by」)試圖要求用戶輸入輸入列表,然後選擇分數最高的玩家。我認爲你是對的,他需要一個Read類型的實例,或者一個函數來解析輸入並將其轉換爲頂部定義的類型。這也取決於如何定義NombreJug,PuntosLogrados,MinutosJugados。他們是否鍵入同義詞?

mejorJugadorPor也看起來應該是String-> 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 =未定義」得到它來編譯,因爲該功能是不是在您發佈的代碼中定義。

改變我做:

  1. 你的函數armarListaDeTuplasPuntosFecha更好用地圖表示。 Map將一個函數應用於列表中的每個元素,這就是您手動執行的操作。

  2. jugadorConMayorCantidadDePuntoEnFecha可以與FST,它返回兩個值

  3. mejorJugadorPor需要是在IO單子,因爲它執行輸入/輸出操作(在用戶閱讀的東西的元組的第一個元素表示類型)。您可以通過將返回類型從String更改爲IO String來實現此目的,即返回值取決於IO(即該函數不是純粹的)。

  4. 函數readLn做你想做的事情,因爲只要類型有一個Read實例,它就會將輸入字符串轉換爲正確的類型。 Read類的基本意思是你可以以某種方式將字符串轉換爲類型的值。

  5. 因爲mejorJugadorPor是monadic,所以您需要確保它返回的值包含在IO monad中。這就是函數返回所做的事情:它取得一個類型「a」的值並將其轉換爲類型「m a」的值,其中m是任何monad。

0

幾個小時後,我擺脫了困境:在英國的幫助下(Julian Porter:www.jpembedded.co.uk,www.porternet.org)。有沒有創建單子或修改類(我不是在該級別還)的方式:

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" 

它在西班牙語。如果有人發現它有用,請聯繫我,我會將其翻譯成英文。

非常感謝那些對這個問題發表評論以及他們的建議。

相關問題