Old lady swallowed a fly: Difference between revisions

m
→‎{{header|Haskell}}: Minor tweaks - mainly importing only tails, rather than whole Data.List library
m (→‎{{header|Haskell}}: Minor tweaks - mainly importing only tails, rather than whole Data.List library)
Line 924:
{{Works with|GHC|7.8.3}}
 
<lang haskell>#!/usr/bin/envimport runhaskellData.List (tails)
 
import Data.List
 
-- Once means the phrase is only printed in the verse about that animal.
Line 934 ⟶ 932:
-- 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
= Once
| Every
| Die
 
animals =
animals = [("horse", Die, "She's dead, of course!"),
[ ("donkeyhorse", OnceDie, "ItShe's wasdead, ratherof wonky. To swallow a donkey.course!"),
, ("cowdonkey", Once, "IIt don'twas knowrather howwonky. To swallow a cowdonkey."),
, ("goatcow", Once, "SheI justdon't openedknow her throathow. To swallow a goatcow."),
, ("piggoat", Once, "HerShe mouthjust wasopened soher bigthroat. To swallow a piggoat."),
, ("dogpig", Once, "WhatHer amouth hogwas so big. To swallow a dogpig."),
, ("catdog", Once, "FancyWhat thata hog. To swallow a catdog."),
, ("birdcat", Once, "QuiteFancy absurdthat. To swallow a birdcat."),
, ("spiderbird", Once, "ThatQuite wriggledabsurd. andTo jiggledswallow anda tickled inside herbird."),
, ("flyspider", EveryOnce, "IThat don'twriggled knowand whyjiggled sheand swallowedtickled theinside flyher.")]
, ("fly", Every, "I don't know why she swallowed the fly.")
]
 
verse :: [(String, AnimalAction, String)] -> [String]
verse ((anim, act, phrase):restAnims) =
let lns = ["I know an old lady who swallowed a " ++ anim ++ ".", phrase]
in case act of Die -> lns
_Die -> lns ++ verse' restAnims anim
_ -> lns ++ verse' restAnims anim
 
verse' :: [(String, AnimalAction, String)] -> String -> [String]
Line 957 ⟶ 961:
verse' ((anim, act, phrase):restAnims) prevAnim =
let why = "She swallowed the " ++ prevAnim ++ " to catch the " ++ anim ++ "."
lns = case act of Every -> [why, phrase]
case act _ -> [why]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>
 
9,659

edits