Created
July 25, 2015 05:14
-
-
Save davenportw15/e4deee8654f65734fb7f to your computer and use it in GitHub Desktop.
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 EvaluateSandwich | |
( SandwichComponent | |
, Sandwich | |
, validateSandwich | |
, indicesSeparated | |
) where | |
import Data.List (elemIndices) | |
-- | Components of a sandwich | |
-- Allows for arbitrary fillings through `Filling String` | |
data SandwichComponent | |
= Bread | |
| Meat | |
| Cheese | |
| Filling String | |
deriving (Eq, Show, Read) | |
-- | A sandwich is a list of sandwich components | |
type Sandwich = [SandwichComponent] | |
-- | Validates that a sandwich has at least | |
-- one filling between two slices of bread. | |
-- Valid sandwiches can contain components | |
-- outside of these two slices of bread | |
-- (This includes other slices of bread) | |
validateSandwich :: Sandwich -> Bool | |
validateSandwich = indicesSeparated . elemIndices Bread | |
-- | Validates that at least two indices are | |
-- separated by at least one index | |
indicesSeparated :: (Integral a) => [a] -> Bool | |
indicesSeparated indices | |
| length indices < 2 = False | |
| otherwise = | |
let first : second : rest = indices in | |
second - first > 1 || indicesSeparated (second : rest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: