趣味编程:静夜思(Haskell版)

时间:2015-05-15 21:24:37   收藏:0   阅读:159
-- jys.hs

import Data.Map (fromListWith, toAscList)
import Data.List (intersperse)

(|>) x f = f x
sortAndGroup kv = fromListWith (++) [(k, [v]) | (k, v) <- kv]
sortAndGroupBy f xs = sortAndGroup $ zip (map f xs) xs

verticalWriting text offset = zip [0..] text
    |> sortAndGroupBy ((`mod` offset) . fst)
    |> toAscList
    |> map snd 
    |> map (map snd)
    |> map (intersperse '|')
    |> unlines
    
main = putStrLn $ verticalWriting "床前明月光疑是地上霜举头望明月低头思故乡" 5

--低|举|疑|床
--头|头|是|前
--思|望|地|明
--故|明|上|月
--乡|月|霜|光

原文:http://blog.csdn.net/zwvista/article/details/45749291

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!