Skip to content

Instantly share code, notes, and snippets.

@sshine
Created November 22, 2011 00:18
Show Gist options
  • Save sshine/1384455 to your computer and use it in GitHub Desktop.
Save sshine/1384455 to your computer and use it in GitHub Desktop.
Cowsay (Haskell)
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