मैं हास्केल में एक साधारण cat
प्रोग्राम लिखने की कोशिश कर रहा हूं। मैं कई फ़ाइल नामों को तर्क के रूप में लेना चाहता हूं, और STDOUT पर प्रत्येक फ़ाइल को अनुक्रमिक रूप से लिखना चाहता हूं, लेकिन मेरा प्रोग्राम केवल एक फ़ाइल प्रिंट करता है और बाहर निकलता है।मैं हास्केल में 'बिल्ली' को कैसे कार्यान्वित करूं?
मेरे कोड को प्रत्येक फ़ाइल को मुद्रित करने के लिए मुझे क्या करने की ज़रूरत है, न केवल पहले व्यक्ति में?
import Control.Monad as Monad
import System.Exit
import System.IO as IO
import System.Environment as Env
main :: IO()
main = do
-- Get the command line arguments
args <- Env.getArgs
-- If we have arguments, read them as files and output them
if (length args > 0) then catFileArray args
-- Otherwise, output stdin to stdout
else catHandle stdin
catFileArray :: [FilePath] -> IO()
catFileArray files = do
putStrLn $ "==> Number of files: " ++ (show $ length files)
-- run `catFile` for each file passed in
Monad.forM_ files catFile
catFile :: FilePath -> IO()
catFile f = do
putStrLn ("==> " ++ f)
handle <- openFile f ReadMode
catHandle handle
catHandle :: Handle -> IO()
catHandle h = Monad.forever $ do
eof <- IO.hIsEOF h
if eof then do
hClose h
exitWith ExitSuccess
else
hGetLine h >>= putStrLn
मैं इस तरह कोड चला रहा हूँ:
runghc cat.hs file1 file2
मैंने आपके स्वीकृत उत्तर को स्विच कर दिया क्योंकि आपने मेरी बग तय की और आलसी आईओ स्ट्रीमिंग को भी समझाया। – Sam
आप कैसे कहते हैं '> => '? – Sam
"क्लेस्ली संरचना"। मुझे इसके लिए कोई बेहतर (छोटा) नाम नहीं पता है। – shang