コンテキストでの型の操作 Monad型クラス リストモナドとリスト内包 リスト内包
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT5(コンテキストでの型の操作)、LESSON32(リストモナドとリスト内包)、32.2(リスト内包)、クイックチェック 32-3の解答を求めてみる。
コード
import Data.Char (toUpper)
colors :: [String]
colors =
  [ "brown",
    "blue",
    "pink",
    "orange"
  ]
names :: [String]
names =
  [ mconcat ["Mr. ", toUpper (head color) : tail color]
    | color <- colors
  ]
main :: IO ()
main = do
  print names
  mapM_ print names
入出力結果(Terminal, Zsh)
% runghc sample03.hs 
["Mr. Brown","Mr. Blue","Mr. Pink","Mr. Orange"]
"Mr. Brown"
"Mr. Blue"
"Mr. Pink"
"Mr. Orange"
%