Estoy tratando de leer n
líneas de contenido en una lista de cadenas. Probé varias variaciones del código a continuación, pero nada funcionó.Lee n líneas en [String]
main = do
input <- getLine
inputs <- mapM getLine [1..read input]
print $ length input
Esto arroja el siguiente error:
Couldn't match expected type `a0 -> IO b0'
with actual type `IO String'
In the first argument of `mapM', namely `getLine'
In a stmt of a 'do' block: inputs <- mapM getLine [1 .. read input]
In the expression:
do { input <- getLine;
inputs <- mapM getLine [1 .. read input];
print $ length input }
Y
main = do
input <- getLine
let inputs = map getLine [1..read input]
print $ length input
tiros
Couldn't match expected type `a0 -> b0'
with actual type `IO String'
In the first argument of `map', namely `getLine'
In the expression: map getLine [1 .. read input]
In an equation for `inputs': inputs = map getLine [1 .. read input]
¿Cómo puedo hacer esto?
Por cierto, 'inputs <- mapM (const getLine) [1 .. read input]' hace exactamente lo que necesita. El problema es que tratas de mapear 'getLine' sobre números' [1 .. n] 'pero' getLine' no es una función. Al usar 'const', lo convierte en una función que simplemente ignora su primer argumento. – Vitus
@Vitus que es digno de ser otra respuesta, la haré +1. – vikingsteve