में एन लाइनें पढ़ें मैं स्ट्रिंग्स की सूची में n
सामग्री की रेखाओं को पढ़ने की कोशिश कर रहा हूं। मैंने नीचे दिए गए कोड के कई बदलावों की कोशिश की है, लेकिन कुछ भी काम नहीं किया है।एन [स्ट्रिंग]
main = do
input <- getLine
inputs <- mapM getLine [1..read input]
print $ length input
यह निम्न त्रुटि फेंकता है:
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 }
और
main = do
input <- getLine
let inputs = map getLine [1..read input]
print $ length input
फेंकता
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]
मैं यह कैसे कर सकते?
वैसे, 'इनपुट <- मैपएम (कॉन्स गेटलाइन) [1 .. इनपुट पढ़ें]' वही है जो आपको चाहिए। समस्या यह है कि आप संख्याओं पर 'getLine' को मानचित्रित करने का प्रयास करते हैं' [1 .. n] 'लेकिन' getLine' एक फ़ंक्शन नहीं है। 'Const' का उपयोग करके, आप इसे उस फ़ंक्शन में बदल देते हैं जो इसके पहले तर्क को अनदेखा करता है। – Vitus
@ विटस जो एक और उत्तर के रूप में योग्य है, मैं इसे +1 कर दूंगा। – vikingsteve