Old lady swallowed a fly: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: Applied hlint, hindent)
Line 1,138: Line 1,138:
<lang haskell>import Data.List (tails)
<lang haskell>import Data.List (tails)


animals=
animals :: [String]
animals =
["fly.\nI don't know why she swallowed a fly.\nPerhaps she'll die.\n"
[ "fly.\nI don't know why she swallowed a fly.\nPerhaps she'll die.\n"
,"spider.\nThat wiggled and jiggled and tickled inside her."
,"bird.\t\nHow absurd, to swallow a bird."
, "spider.\nThat wiggled and jiggled and tickled inside her."
,"cat.\t\nImagine that. She swallowed a cat."
, "bird.\t\nHow absurd, to swallow a bird."
,"dog.\t\nWhat a hog to swallow a dog."
, "cat.\t\nImagine that. She swallowed a cat."
,"goat.\t\nShe just opened her throat and swallowed a goat."
, "dog.\t\nWhat a hog to swallow a dog."
,"cow.\nI don't know how she swallowed a cow."
, "goat.\t\nShe just opened her throat and swallowed a goat."
,"horse.\nShe's dead, of course."]
, "cow.\nI don't know how she swallowed a cow."
, "horse.\nShe's dead, of course."
]


beginnings :: [String]
beginnings = map ("There was an old lady who swallowed a "++) animals
beginnings = ("There was an old lady who swallowed a " ++) <$> animals
lastVerse = reverse $ ["She swallowed the "++takeWhile (/='.') y++" to catch the "++ takeWhile (/='\t') x

| (x:y:_:_)<-tails animals]
lastVerse :: [String]
main = putStr $ concatMap unlines $ zipWith (:) beginnings $ reverse $ ([]:) $ tails $ lastVerse</lang>
lastVerse =
reverse
[ "She swallowed the " ++
takeWhile (/= '.') y ++ " to catch the " ++ takeWhile (/= '\t') x
| (x:y:_:_) <- tails animals ]

main :: IO ()
main =
putStr $
concatMap unlines $
zipWith (:) beginnings $ reverse $ ([] :) (tails lastVerse)</lang>
{{out}}<pre>There was an old lady who swallowed a fly.
{{out}}<pre>There was an old lady who swallowed a fly.
I don't know why she swallowed a fly.
I don't know why she swallowed a fly.