Old lady swallowed a fly: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Haskell}}: shorter version) |
||
Line 1,046: | Line 1,046: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
⚫ | |||
{{Works with|GHC|7.8.3}} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- Once means the phrase is only printed in the verse about that animal. |
|||
⚫ | |||
-- Every means the phrase is printed for every verse. It is used for "fly", |
|||
lastVerse = tail $ reverse $ zipWith swalloed animals $ tail animals |
|||
-- and could optionally be used for "spider", in the version of the song where |
|||
main = putStr $ concatMap unlines $ zipWith (:) beginnings $ cycle $ reverse $ tails $ lastVerse</lang> |
|||
-- "wriggled and jiggled..." is repeated every verse. |
|||
-- Die is only used for the horse, and means the chain of animals won't be |
|||
-- included in the verse. |
|||
data AnimalAction |
|||
= Once |
|||
| Every |
|||
| Die |
|||
⚫ | |||
⚫ | |||
, ("donkey", Once, "It was rather wonky. To swallow a donkey.") |
|||
⚫ | |||
⚫ | |||
, ("pig", Once, "Her mouth was so big. To swallow a pig.") |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
] |
|||
verse :: [(String, AnimalAction, String)] -> [String] |
|||
verse ((anim, act, phrase):restAnims) = |
|||
⚫ | |||
in case act of |
|||
Die -> lns |
|||
_ -> lns ++ verse' restAnims anim |
|||
verse' :: [(String, AnimalAction, String)] -> String -> [String] |
|||
verse' [] _ = ["Perhaps she'll die."] |
|||
verse' ((anim, act, phrase):restAnims) prevAnim = |
|||
⚫ | |||
lns = |
|||
case act of |
|||
Every -> [why, phrase] |
|||
_ -> [why] |
|||
in lns ++ verse' restAnims anim |
|||
song :: [String] |
|||
song = concatMap verse . tail . reverse $ tails animals |
|||
main :: IO () |
|||
main = putStr $ unlines song</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |