Sha256: eee677c11eefa555bf082edd84c94f25ef7ef87fccd7b7b77f3e2732b8013e6d

Contents?: true

Size: 1.74 KB

Versions: 302

Compression:

Stored size: 1.74 KB

Contents

module FoodChain (song) where

import Data.Array (Array, Ix, (!), listArray)
import Data.Char (toLower)
import Text.Printf (printf)

data Animal = Fly | Spider | Bird | Cat | Dog | Goat | Cow | Horse
              deriving (Eq, Enum, Ix, Ord, Show)

-- | An array that contains the lowercase string representation for all
-- animals. E.g. animalNames ! Fly == "fly"
animalNames :: Array Animal String
animalNames = listArray (Fly, Horse) $ map showLower [Fly ..]
  where showLower a = let (h:t) = show a
                      in (toLower h : t)

song :: String
song = init . unlines $ map verse [Fly ..]

verse :: Animal -> String
verse animal = swallow animal ++ unlines (map catches prey)
  where prey | animal `elem` [Fly, Horse] = []
             | otherwise =  [animal, pred animal .. Spider]

swallow :: Animal -> String
swallow animal = printf "I know an old lady who swallowed a %s.\n%s\n"
                        (animalNames ! animal) (comment animal)

catches :: Animal -> String
catches animal = printf "She swallowed the %s to catch the %s%s"
                        (animalNames ! animal) (animalNames ! prev) (end prev)
  where prev = pred animal
        end Fly    = ".\n" ++ comment Fly
        end Spider = " that wriggled and jiggled and tickled inside her."
        end _      = "."

comment :: Animal -> String
comment animal = case animal of
  Fly    -> "I don't know why she swallowed the fly. Perhaps she'll die."
  Spider -> "It wriggled and jiggled and tickled inside her."
  Bird   -> "How absurd to swallow a bird!"
  Cat    -> "Imagine that, to swallow a cat!"
  Dog    -> "What a hog, to swallow a dog!"
  Goat   -> "Just opened her throat and swallowed a goat!"
  Cow    -> "I don't know how she swallowed a cow!"
  Horse  -> "She's dead, of course!" 

Version data entries

302 entries across 302 versions & 1 rubygems

Version Path
trackler-2.2.1.84 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.83 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.82 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.81 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.80 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.79 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.78 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.77 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.76 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.75 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.74 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.73 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.72 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.71 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.70 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.69 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.68 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.67 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.66 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs
trackler-2.2.1.65 tracks/haskell/exercises/food-chain/examples/success-standard/src/FoodChain.hs