Created
November 22, 2011 00:18
-
-
Save sshine/1384455 to your computer and use it in GitHub Desktop.
Cowsay (Haskell)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.String.Utils | |
cow :: String | |
cow = concatMap (++ "\n") [ | |
" \\ ^__^", | |
" \\ (oo)\\_______", | |
" (__)\\ )\\/\\", | |
" ||----w |", | |
" || ||" ] | |
box :: String -> String | |
box msg = | |
".-" ++ dashes ++ "-.\n" ++ | |
concatMap pad msgs ++ | |
"`-" ++ dashes ++ "-'\n" | |
where | |
msgs = split "\n" msg | |
longest = foldr max 0 (map length msgs) | |
dashes = replicate longest '-' | |
pad s = "| " ++ s ++ replicate (longest - length s) ' ' ++ " |\n" | |
cowsay :: String -> String | |
cowsay msg = box msg ++ cow | |
wordwrap :: Int -> String -> String | |
wordwrap maxlen msg = | |
tail $ ww (words msg) [] 0 | |
where | |
ww (w:ws) res len | |
| length w + len < maxlen = ww ws (res++" "++w) (len+length w) | |
| otherwise = res ++ "\n" ++ ww ws w (length w) | |
ww _ [] _ = "" | |
ww _ res _ = res | |
cowsay_wrapped :: String -> String | |
cowsay_wrapped = cowsay . wordwrap 40 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment