文字列を段落に分ける
段落は大まかにいって2通りの方法で区切られます.
1. 「改行」が区切り子
2. 「空行」が区切り子
この見方は日本語の文書で見られます.
2.の場合は段落を区切り子(空行)を含まない連続した行のリストとみなします.
[*** Text版]
インポート
code: (haskell)
import Data.List (unfoldr)
import qualified Data.Text as T
定義例
code: (haskell)
type Paragraph1 = Text
paragraphs1 = filter (not . T.null) . T.lines
paragraphs2 = unfoldr f . T.lines
where
f [] = Nothing
f ("":xs) = f xs
f xxs = case break T.null xxs of
(ys,zs) -> bool (Just (ys, tail zs)) (Just (ys,zs)) (null zs)
sample1 = pack sample1'
sample2 = pack sample2'
[*** String版]
インポート
code: (haskell)
import Data.List (unfoldr)
定義例
code: (haskell)
type Paragraph1' = String
paragraphs1' = filter (not . null) . lines
paragraphs2' = unfoldr f . lines
where
f [] = Nothing
f ("":xs) = f xs
f xxs = case break T.null xxs of
(ys,zs) -> bool (Just (ys, tail zs)) (Just (ys,zs)) (null zs)
sample1' :: String
sample1' = "山路を登りながら、こう考えた。\n\
\智に働けば角が立つ。情に棹させば流される。意地を通せば窮屈だ。とかくに人の世は住みにくい。\n"
sample2' :: String
sample2' = "Going up a mountain track, I fell to thinking.\n\
\n\
\Approach everything rationally, and you become harsh.\n\
\Pole along in the stream of emotions, and you will be swept away by the current.\n\
\Give free rein to your desires, and you become uncomfortably confined.\n\
\It is not a very agreeable place to live, this world of ours.\n"
評価例
code:_
{-
>> :set -XOverloadedStrings
>> sep1' = "\n-----\n"
>> sep2' = "====="
>> sep1 = T.pack sep1'
>> sep2 = T.pack sep2'
>> T.putStr sample1
山路を登りながら、こう考えた。
智に働けば角が立つ。情に棹させば流される。意地を通せば窮屈だ。とかくに人の世は住みにくい。
>> T.putStr . T.unlines . T.lines $ sample2
Going up a mountain track, I fell to thinking.
Approach everything rationally, and you become harsh.
Pole along in the stream of emotions, and you will be swept away by the current.
Give free rein to your desires, and you become uncomfortably confined.
It is not a very agreeable place to live, this world of ours.
>> T.putStr . T.intercalate sep1 $ paragraphs1 sample1
山路を登りながら、こう考えた。
-----
智に働けば角が立つ。情に棹させば流される。意地を通せば窮屈だ。とかくに人の世は住みにくい。
>> T.putStr . T.concat . map T.unlines $ L.intersperse sep2 $ paragraphs2 sample2 Going up a mountain track, I fell to thinking.
=====
Approach everything rationally, and you become harsh.
Pole along in the stream of emotions, and you will be swept away by the current.
Give free rein to your desires, and you become uncomfortably confined.
It is not a very agreeable place to live, this world of ours.
>> putStr sample1'
山路を登りながら、こう考えた。
智に働けば角が立つ。情に棹させば流される。意地を通せば窮屈だ。とかくに人の世は住みにくい。
>> putStr . unlines . lines $ sample2'
Going up a mountain track, I fell to thinking.
Approach everything rationally, and you become harsh.
Pole along in the stream of emotions, and you will be swept away by the current.
Give free rein to your desires, and you become uncomfortably confined.
It is not a very agreeable place to live, this world of ours.
>> putStr . L.intercalate sep1' $ paragraphs1' sample1'
山路を登りながら、こう考えた。
-----
智に働けば角が立つ。情に棹させば流される。意地を通せば窮屈だ。とかくに人の世は住みにくい。
>> putStr . concat . map unlines $ L.intersperse sep2' $ paragraphs2' sample2' Going up a mountain track, I fell to thinking.
=====
Approach everything rationally, and you become harsh.
Pole along in the stream of emotions, and you will be swept away by the current.
Give free rein to your desires, and you become uncomfortably confined.
It is not a very agreeable place to live, this world of ours.