Ich versuche n
Zeilen mit Inhalt in eine Liste von Strings zu lesen. Ich habe verschiedene Varianten des Codes unten versucht, aber nichts hat funktioniert.Lesen von n Zeilen in eine [Zeichenfolge]
main = do
input <- getLine
inputs <- mapM getLine [1..read input]
print $ length input
Dies führt den folgenden Fehler:
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 }
Und
main = do
input <- getLine
let inputs = map getLine [1..read input]
print $ length input
wirft
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]
Wie kann ich das tun?
Übrigens, 'Eingänge <- mapM (const getLine) [1 .. Leseeingang]' macht genau das, was Sie brauchen. Das Problem ist, dass Sie 'getLine' über Zahlen' [1 .. n] 'abbilden, aber' getLine' ist keine Funktion. Mit 'const' verwandeln Sie es in eine Funktion, die nur das erste Argument ignoriert. – Vitus
@Vitus, das als eine andere Antwort würdig ist, werde ich es +1. – vikingsteve