2006-08-25
■ [ふつケル] 8.6節 練習問題
まずは自力で
lstrip :: String -> String lstrip = dropWhile matchSpace matchSpace :: Char -> Bool matchSpace c = any (== c) [' ', '\t'] rstrip :: String -> String rstrip = reverse . lstrip . reverse strip :: String -> String strip = lstrip . rstrip
main = do cs <- getContents
putStrLn $ lastNLines 10 cs
lastNLines n = unlines . takeLast n . lines
takeLast n = reverse . take n . reverse
解答を写経
import Char lstrip :: String -> String lstrip = dropWhile isSpace rstrip :: String -> String rstrip = reverse . lstrip . reverse strip :: String -> String strip = rstrip . lstrip
main = do cs <- getContents
putStr $ lastNLines 10 cs
lastNLines n = unlines . takeLast n . lines
takeLast n = reverse . take n . reverse
isSpaceを自作しちゃった以外は上出来じゃない?matchSpaceは、簡単にスペース文字を追加できるようになってるから、あとからスペース文字を増やすこともできるし、セクションの勉強にもなったし、結果オーライってことで。
Mohammad2012/05/02 18:21I ltieallry jumped out of my chair and danced after reading this!
pavgsqfj2012/05/03 11:28xqgVIZ <a href="http://qjelzltbtkpo.com/">qjelzltbtkpo</a>
lyvvjyxlh2012/05/03 15:13K6fhGU , [url=http://ubxrucmfpltn.com/]ubxrucmfpltn[/url], [link=http://gughlccdruix.com/]gughlccdruix[/link], http://erinivqtnqns.com/
zqefrc2012/05/04 15:45jVpRsW <a href="http://egcyxsybgnsz.com/">egcyxsybgnsz</a>
jcisbfksmc2012/05/05 16:04pcPkE8 , [url=http://wjbzmimoanyc.com/]wjbzmimoanyc[/url], [link=http://rcfbqnmfncrw.com/]rcfbqnmfncrw[/link], http://saucybmfgegy.com/
jcisbfksmc2012/05/05 16:05pcPkE8 , [url=http://wjbzmimoanyc.com/]wjbzmimoanyc[/url], [link=http://rcfbqnmfncrw.com/]rcfbqnmfncrw[/link], http://saucybmfgegy.com/