Last active
May 31, 2016 00:54
-
-
Save Shamrock-Frost/57de242af2b32ec89fc6250a0dde3422 to your computer and use it in GitHub Desktop.
Biology extra credit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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