Sto provando a leggere le righe di contenuto n
in un elenco di stringhe. Ho provato diverse varianti del codice qui sotto, ma non ha funzionato.Leggi n righe in una [stringa]
main = do
input <- getLine
inputs <- mapM getLine [1..read input]
print $ length input
Questo getta il seguente errore:
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 }
E
main = do
input <- getLine
let inputs = map getLine [1..read input]
print $ length input
tiri
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]
Come posso fare questo?
A proposito, 'input <- mapM (const getLine) [1 .. read input]' fa esattamente quello che ti serve. Il problema è che si tenta di mappare 'getLine' su numeri' [1 .. n] 'ma' getLine' non è una funzione. Usando 'const', lo trasformi in una funzione che ignora il suo primo argomento. – Vitus
@Vitus che è degno come un'altra risposta, io lo faccio +1. – vikingsteve