Haskellで出力する
参考
「アルゴ式」をHaskellで学ぶための準備
table:table
関数 出力先 改行 型クラスの制約
print 標準出力 あり Show
putStr 標準出力 なし なし
putStrLn 標準出力 あり なし
show 文字列に変換 なし Show
hPutStr 任意のハンドル なし なし
hPutStrLn 任意のハンドル あり なし
文字列を出力
putStrLn
putStrLn :: String -> IO ()
code:hs
main = do
putStrLn s
数値を出力
print :: Show a => a -> IO ()
code:hs
main = do
print n
文字列に対して使うとダブルクオーテーションがついてしまう
リストを複数行に出力する
code:hs
main = do
mapM_ print ns
mapM_ printStrLn xs
リストを空白区切りで出力する
code:hs
main = do
putStrLn . unwords . map show $ (ns :: Int)
GPT-4.icon
hPutStrとhPutStrLn
これらは特定のハンドル(例: ファイルや標準エラー)に対して文字列を出力するための関数です。
型:
hPutStr :: Handle -> String -> IO ()
hPutStrLn :: Handle -> String -> IO ()
用途: 標準出力以外のハンドルを操作します。
例:
code:haskell
import System.IO
hPutStrLn stderr "Error: Something went wrong"
-- 標準エラー出力にメッセージを出力