Skip to content

Instantly share code, notes, and snippets.

@Shamrock-Frost
Last active May 31, 2016 00:54
Show Gist options
  • Save Shamrock-Frost/57de242af2b32ec89fc6250a0dde3422 to your computer and use it in GitHub Desktop.
Save Shamrock-Frost/57de242af2b32ec89fc6250a0dde3422 to your computer and use it in GitHub Desktop.
Biology extra credit
To add more phyla, put a '|' and then the Phylum's name in Phyla.hs.
To create a Key, you use 'Either', followed by a question, then two things that can be either a terminating Phylum or another Key (created in the same way).
To create a terminating Phylum, enter "Answer <Phylum name>"
Every time you add another level of the key you need parentheses.
module Main (main) where
import Data.Char (toLower)
import Prelude hiding (Either)
import Phyla
data Key a = Answer a | Either Question (Key a) (Key a)
instance (Show a) => Show (Key a) where show (Answer x) = show x
type Question = String
phyloKey :: Key Phylum
phyloKey = Either "Does it have an irregular body form?" (Answer Porifera) (
Either "Does it have radial symmetry?" (
Either "Is its body leathery?" (Answer Echinodermata) (Answer Cnidaria_Or_Ctenophora)
) (
Either "Does it have present gills, a spine, or appendages in pairs of two?" (Answer Chordata) (
Either "Does it have jointed appendages or an exoskeleton?" (Answer Arthropoda) (
Either "Is its body flat, soft, or wormlike?" (Answer Platyhelminthes) (
Either "Is its body wormlike with conspicuous segments?" (Answer Annelida) (
Either "Is its body long, slender, smooth, and wormlike?" (Answer Nematoda) (Answer Mollusca)
)
)
)
)
)
)
getToTheBottomOf :: Key a -> IO a
getToTheBottomOf (Answer x) = return x
getToTheBottomOf (Either q t1 t2) = do
print q
getLine >>= parse
where parse str = case (map toLower . take 1) str of
"y" -> getToTheBottomOf t1
"n" -> getToTheBottomOf t2
otherwise -> do
putStrLn "Sorry, Yes or No answers only."
getToTheBottomOf (Either q t1 t2)
main = getToTheBottomOf phyloKey
module Phyla where
data Phylum = Porifera
| Echinodermata
| Cnidaria_Or_Ctenophora
| Chordata
| Arthropoda
| Platyhelminthes
| Annelida
| Nematoda
| Mollusca
deriving Show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment