Haskellで出力する
参考
table:table
関数 出力先 改行 型クラスの制約
show 文字列に変換 なし Show
文字列を出力
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 :: Handle -> String -> IO ()
hPutStrLn :: Handle -> String -> IO ()
用途: 標準出力以外のハンドルを操作します。
例:
code:haskell
import System.IO
hPutStrLn stderr "Error: Something went wrong"
-- 標準エラー出力にメッセージを出力