インタープリターに貼り付けるだけで、Python でアドホックに文字列を解析するのが好きです。
>>> s = """Adams, John
... Washington,George
... Lincoln,Abraham
... Jefferson, Thomas
... """
>>> print "\n".join(x.split(",")[1].replace(" ", "")
for x in s.strip().split("\n"))
John
George
Abraham
Thomas
これは Python インタープリターを使用するとうまく機能しますが、私はこれを Haskell/GHCi で実行したいと考えています。問題は、複数行の文字列を貼り付けられないことです。EOF 文字で getContents を使用できますが、EOF 文字は stdin を閉じるため、一度しか実行できません。
Prelude> s <- getContents
Prelude> s
"Adams, John
Adams, John\nWashington,George
Washington,George\nLincoln,Abraham
Lincoln,Abraham\nJefferson, Thomas
Jefferson, Thomas\n^Z
"
Prelude> :{
Prelude| putStr $ unlines $ map ((filter (`notElem` ", "))
Prelude| . snd . (break (==','))) $ lines s
Prelude| :}
John
George
Abraham
Thomas
Prelude> x <- getContents
*** Exception: <stdin>: hGetContents: illegal operation (handle is closed)
GHCiでこれを行うためのより良い方法はありますか? 注 - getContents (および一般的な Haskell IO) に関する私の理解は、おそらく深刻に壊れています。
更新しました
私は受け取った答えで遊んでいます。ここに私が作成した(盗作した)いくつかのヘルパー関数があります。これは、ephemientの回答からのPythonの"""
引用(開始ではなく、で終わることによって)をシミュレートします。"""
getLinesWhile :: (String -> Bool) -> IO String
getLinesWhile p = liftM unlines $ takeWhileM p (repeat getLine)
getLines :: IO String
getLines = getLinesWhile (/="\"\"\"")
GHCi で AndrewC の回答を使用するには -
C:\...\code\haskell> ghci HereDoc.hs -XQuasiQuotes
ghci> :{
*HereDoc| let s = [heredoc|
*HereDoc| Adams, John
*HereDoc| Washington,George
*HereDoc| Lincoln,Abraham
*HereDoc| Jefferson, Thomas
*HereDoc| |]
*HereDoc| :}
ghci> putStrLn s
Adams, John
Washington,George
Lincoln,Abraham
Jefferson, Thomas
ghci> :{
*HereDoc| putStr $ unlines $ map ((filter (`notElem` ", "))
*HereDoc| . snd . (break (==','))) $ lines s
*HereDoc| :}
John
George
Abraham
Thomas