Im Anfänger und im Versuch, Dateien von meinem Ordner „Papiere“ (im selben Pfad, der die Skripte) zu lesen, aber ich die nächsten Fehler habe:Wie kann ich Dateien in Haskell lesen?
Couldn't match expected type ‘[FilePath]’
with actual type ‘IO [FilePath]’
In the first argument of ‘leerDocumentos’, namely ‘pathFicheros’
In the first argument of ‘return’, namely
‘(leerDocumentos pathFicheros)’
Vielleicht ist das nicht der Fehler, ich habe auf meinem Code, so hoffe ich, dass jemand mir helfen kann, mein Modul zu vervollständigen.
Der Code meiner Leser:
module LeerDocumentos2 where
import System.Directory
import System.IO.Unsafe
import System.IO()
import Documento
reader :: IO [Documento]
reader = do
setCurrentDirectory "papers"
directorio <- getCurrentDirectory
putStrLn directorio -- Directorio donde estan los documentos
let pathFicheros = getDirectoryContents directorio
return (leerDocumentos pathFicheros)
leerDocumentos :: [FilePath] -> [Documento]
leerDocumentos [] = []
leerDocumentos (x:xs) = do
let documento = unsafePerformIO (leerDocumento x)
[documento]++ leerDocumentos xs
leerDocumento :: String -> IO Documento
leerDocumento ruta = do
putStrLn ruta
texto <- readFile ruta
let docuAux = lines texto
let revista = obtenerRevista docuAux
let idd = obtenerID docuAux
let anno = obtenerAnno docuAux
let titulo = obtenerTitulo docuAux
let resumen = obtenerResumen docuAux
let secciones = obtenerSecciones docuAux
let documento = D (revista,idd,anno,titulo,resumen,secciones)
return documento
obtenerRevista :: [String] -> String
obtenerRevista [] = []
obtenerRevista texto = head texto
obtenerID:: [String] -> String
obtenerID [] = []
obtenerID texto = head (drop 1 (texto))
obtenerAnno:: [String] -> String
obtenerAnno [] = []
obtenerAnno texto = head (drop 2 (texto))
obtenerTitulo:: [String] -> String
obtenerTitulo [] = []
obtenerTitulo texto = head (drop 4 (texto))
obtenerResumen:: [String] -> String
obtenerResumen [] = []
obtenerResumen texto = head (drop 6 (texto))
obtenerSecciones :: [String]->[String]
obtenerSecciones [] = []
obtenerSecciones texto = quitarSeparador (drop 8 (texto))
quitarSeparador :: [String] -> [String]
quitarSeparador [] = []
quitarSeparador (s:sn) = if s == "--" || length s <= 1 then --Para quitar lineas blancas
quitarSeparador sn
else
s:quitarSeparador sn
und dies ist der Code meines Modul Documento:
Modul Documento wo
data Documento = Documento {revista :: String, idD :: String, anno :: String, titulo :: String, resumen :: String, secciones :: [String]} deriving (Eq)
type Documentos = [Documento]
Dank !!
Zuerst bitte vergessen Sie, dass "unsafePerfomIO" überhaupt existiert und verwenden Sie dann 'pathFicheros <- getDirectoryContents directorio' anstelle von' let' – Carsten